第二个重写规则在 htaccess 中不起作用,并显示 404 未找到页面

Second rewriterule is not working in htaccess and shows 404 not found page

嗨,我有 link 个像这样的:

www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html

a字母和b字母文件夹下也有3个文件。我用这个 .htaccess 代码删除了 a-letter 部分:

##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ / [L,R=301,NC,NE]
##

并删除了带有此 .htaccess 代码的 b-letter 部分:

##
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ / [L,R=301,NC,NE]
##

我正在使用此 htaccess 代码重定向所有 a-1 a-2 a-3 文件:

RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ / [R=301,L]
RequestCond %{DOCUMENT_ROOT}/a-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(a-1|a-2|a-3)/? /a-letter/.html [END]

此外,我正在使用此 htaccess 代码重定向所有 b-1 b-2 b-3 文件:

##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|\.html)?$ / [R=301,L]
RequestCond %{DOCUMENT_ROOT}/b-letter%{REQUEST_URI}.html -f
RewriteRule ^/?(b-1|b-2|b-3)/? /b-letter/.html [END]
##

所有这些代码都在同一个 .htaccess 文件中,当我想打开时 www.example.com/a-1.html 页面正确打开。我的问题是虽然我对 b-letter 使用相同的代码,但我无法打开 link www.example.com/b-1 。我收到 404 未找到错误。这次当我将 b 字母的代码替换到 .htaccess 文件的顶部时 www.example.com/b-1 正确打开但是 www.example.com/a-1 link 没有打开 .htaccess 文件下面的代码.我试图将 [END] 标志更改为 [L] 它对我不起作用。在这种情况下我能做什么?有什么方法可以禁用 .htaccess 文件中排序的重要性。我在 Whosebug 和其他网站上做了很多研究,但找不到任何适合我的结果。如果你能帮助我,我将不胜感激。从现在开始谢谢。

更新:

应@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/ [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 ^(.+)$ .html [L,QSA]
##

#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ / [R=301,L]
##

------------------------------------------------------------------------


# remove a-letter from URLs start#
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ / [L,R=301,NC,NE]
# remove a-letter from URLs end#

## Rewrite non a-letter links start##
RewriteEngine on
RewriteRule ^/?a-letter/(.*)(/|\.html)?$ / [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/.html [END]
## Rewrite non a-letter links start##

# remove comments/a/ from links start#
RewriteCond %{THE_REQUEST} comments/a/ [NC]
RewriteRule ^comments/a/(.*)$ / [L,R=301,NC,NE]
# remove comments/a/ from links end#


## Rewrite non comments/a/ links start##
RewriteEngine on
RewriteRule ^/?comments/a/(.*)(/|\.html)?$ / [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/a%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?]+)$/? /comments/a/.html [END]
##Rewrite non comments/a/ links end##

------------------------------------------------------------------------


# remove b-letter from URLs start#
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ / [L,R=301,NC,NE]
# remove b-letter from URLs end#

## Rewrite non b-letter links start##
RewriteEngine on
RewriteRule ^/?b-letter/(.*)(/|\.html)?$ / [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/.html [END]
##Rewrite non b-letter links end##



#remove comments/b/ from links start#
RewriteCond %{THE_REQUEST} comments/b/ [NC]
RewriteRule ^comments/b/(.*)$ / [L,R=301,NC,NE]
# remove comments/b/ from links end#    

## Rewrite non comments/b/ links start##
RewriteEngine on
RewriteRule ^/?comments/b/(.*)(/|\.html)?$ / [R=301,L]
RequestCond %{DOCUMENT_ROOT}/comments/b%{REQUEST_URI}.html -f
RewriteRule ^/?([^.?]+)$/? /comments/b/.html [END]
## Rewrite non-comments/b/ links end##

------------------------------------------------------------------------

嗨,我是这样解决我的问题的:

首先我的网站 links 是这样的:

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

我使用以下代码从 link 目录中删除了 a-letter

# remove /a-letter/ from URLs
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ / [L,R=301,NC,NE]

and for b-letter for comments/a/ and comments/b/ like above only letters different.

我在内部为 /a-1 /a-2 /a-3 更改了重写规则,如下所示:(此外,我的托管服务使用低速服务器,因为这个原因我写了 <IfModule LiteSpeed> intead <ifModule mod_rewrite.c>) 的内部代码 www.example.com/a-1 displays www.example.com/a-1.html 的内容。 link.

没有变化
<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/.html [L]
</IfModule>

我也改变了 b-letterb-1 b-2 b-3 就像上面只有字母不同。

我用这段代码在内部重写了非 /comments/a/ links:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/.html [L,NC]

并且对于非 comments/b/ link 仅字母不同。

我的.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 ^(.+)$ .html [L,QSA]
##


##301 from example.com/page.html to example.com/page##
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ / [R=301,L]
##
##Remove .html extension end##
------------------------------------------------------------------------

##remove /a-letter/ from link ##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ / [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/.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/(.*)$ / [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/.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/.html [L,NC]
##Rewrite non /comments/a/ links internally end##
.
.
.
------------------------------------------------------------------------

使用此代码一切正常,我的网站已满 friendly-urls。特别感谢 @arkascha@anubhava 的帮助。