问题描述
嗨,我有这样的链接:
www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html
a Letter和b-Letter文件夹下也有3个文件。我删除了a-letter
部分,并使用以下.htaccess代码:
##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
并删除了带有.htaccess代码的b-letter
部分:
##
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
我正在使用此htaccess代码重定向所有a-1
a-2
a-3
文件:
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2|a-3)/? /a-letter/$1.html [END]
另外,我正在使用此htaccess代码重定向所有b-1
b-2
b-3
文件:
##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/b-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(b-1|b-2|b-3)/? /b-letter/$1.html [END]
##
同一.htaccess文件中的所有这些代码,当我想要打开www.example.com/a-1.html
页面时,它会正确打开。我的问题是,虽然我对b-letter
使用了相同的代码,但我无法打开www.example.com/b-1
的链接。我得到了404未找到错误。当我这次将b字母的代码替换到.htaccess文件的顶部时,www.example.com/b-1
正确打开,但www.example.com/a-1
链接没有打开.htaccess文件中下面的代码。我试图将[结束]标志改为[L],但这对我不起作用。在这种情况下我能做些什么?有没有办法在.htaccess文件中禁用排序的重要性。我在Stackoverflow和其他网站上做了很多研究,但我找不到任何适合我的结果。如果你能帮助我,我将不胜感激。从现在开始谢谢。
更新:
我还应@anubhava的要求在下面分享了我的.htaccess文件的完整代码
#Redirection http to https start#
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
#Redirection http to https end#
##301 Redirection Start##
Redirect 301 /index.html https://www.example.com/
Redirect 301 /index https://www.example.com/
##301 Redirection End##
##adding www to links start##
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301,NC]
##adding www to links end##
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
##
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/
RewriteRule ^(.*).html$ /$1 [R=301,L]
##
------------------------------------------------------------------------
# remove a-letter from URLs start#
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
# remove a-letter from URLs end#
## Rewrite non a-letter links start##
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? /a-letter/$1.html [END]
## Rewrite non a-letter links start##
# remove comments/a/ from links start#
RewriteCond %{THE_REQUEST} comments/a/ [NC]
RewriteRule ^comments/a/(.*)$ /$1 [L,R=301,NC,NE]
# remove comments/a/ from links end#
## Rewrite non comments/a/ links start##
RewriteEngine on
RewriteRule ^/?comments/a/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?]+)$/? /comments/a/$1.html [END]
##Rewrite non comments/a/ links end##
------------------------------------------------------------------------
# remove b-letter from URLs start#
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
# remove b-letter from URLs end#
## Rewrite non b-letter links start##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/b-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(b-1|b-2|b-3|b-4|b-5|b-6|b-7|b-8)/? /b-letter/$1.html [END]
##Rewrite non b-letter links end##
#remove comments/b/ from links start#
RewriteCond %{THE_REQUEST} comments/b/ [NC]
RewriteRule ^comments/b/(.*)$ /$1 [L,R=301,NC,NE]
# remove comments/b/ from links end#
## Rewrite non comments/b/ links start##
RewriteEngine on
RewriteRule ^/?comments/b/(.*)(/|.html)?$ /$1 [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/b%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?]+)$/? /comments/b/$1.html [END]
## Rewrite non-comments/b/ links end##
------------------------------------------------------------------------
推荐答案
Hi我这样解决了我的问题:
首先我的网站链接是这样的:
www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html
www.example.com/comments/a/word-abc.html
www.example.com/comments/b/word-xyz.html
我从链接目录中删除了a-letter
,代码如下:
# remove /a-letter/ from URLs
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
和b-letter
对于上述comments/a/
和comments/b/
,只有字母不同。
我在内部更改了/a-1
/a-2
/a-3
的重写规则:(而且我的托管服务使用了低速服务器,因此我写了<IfModule LiteSpeed>
<ifModule mod_rewrite.c>
),用这个代码www.example.com/a-1在内部显示www.example.com/a-1.html的内容。链接没有变化。
<IfModule LiteSpeed>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? a-letter/$1.html [L]
</IfModule>
我还更改了b-letter
和b-1
b-2
b-3
,只是上面的字母不同。
我用以下代码在内部重写了非/comments/a/
链接:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/$1.html [L,NC]
与上述非comments/b/
链接仅字母不同。
我的.htaccess
文件内容顺序如下:
##Redirect http to https start##
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R=301,L]
##Redirect http to https end##
##301 Redirect Start##
Redirect 301 /index.html https://www.example.com/
Redirect 301 /index https://www.example.com/
##301 Redirect End##
##Remove .html extension start##
##example.com/page will display the contents of example.com/page.html##
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
##
##301 from example.com/page.html to example.com/page##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9} /.*.html HTTP/
RewriteRule ^(.*).html$ /$1 [R=301,L]
##
##Remove .html extension end##
------------------------------------------------------------------------
##remove /a-letter/ from link ##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##
.
.
.
##Rewrite non /a-letter/ links internally start##
<IfModule LiteSpeed>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(a-1|a-2|a-3|a-4|a-5|a-6|a-7|a-8)/? a-letter/$1.html [L]
</IfModule>
##Rewrite non /a-letter/ links internally end##
.
.
.
##remove /comments/a/ from link start##
RewriteCond %{THE_REQUEST} comments/a/ [NC]
RewriteRule ^comments/a/(.*)$ /$1 [L,R=301,NC,NE]
##remove /comments/a/ from link end##
.
.
.
##Rewrite non /comments/a/ links internally start##
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/$1.html [L,NC]
##Rewrite non /comments/a/ links internally end##
.
.
.
------------------------------------------------------------------------
这些代码一切正常,我的网站有完整的友好URL。特别感谢@arkascha
和@anubhava
的帮助。