首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >第二,重写found在htaccess中不工作,并显示404 working页面。

第二,重写found在htaccess中不工作,并显示404 working页面。
EN

Stack Overflow用户
提问于 2022-03-03 11:50:39
回答 1查看 267关注 0票数 1

嗨,我有这样的链接:

代码语言:javascript
复制
www.example.com/a-letter/a-1.html
www.example.com/b-letter/b-1.html

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

代码语言:javascript
复制
##
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]
##

并使用以下b-letter代码删除了.htaccess部分:

代码语言:javascript
复制
##
RewriteCond %{THE_REQUEST} /b-letter/ [NC]
RewriteRule ^b-letter/(.*)$ /$1 [L,R=301,NC,NE]
##

我使用以下htaccess代码重定向所有a-1 a-2 a-3文件:

代码语言:javascript
复制
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文件:

代码语言:javascript
复制
##
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文件中排序的重要性。我做了很多关于堆栈溢出和其他网站的研究,但是我没有找到任何对我有用的结果。如果你能帮助我,我将不胜感激。从现在开始谢谢你。

更新:

我还通过@anubhava的请求共享了下面.htaccess文件的完整代码

代码语言:javascript
复制
#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##

------------------------------------------------------------------------
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-03-05 14:39:28

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

首先,我的网站链接如下:

代码语言:javascript
复制
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

代码语言:javascript
复制
# remove /a-letter/ from URLs
RewriteCond %{THE_REQUEST} /a-letter/ [NC]
RewriteRule ^a-letter/(.*)$ /$1 [L,R=301,NC,NE]

而对于b-lettercomments/a/comments/b/,就像上面只有不同的字母。

我在内部更改了/a-1 /a-2 /a-3的重写规则:(而且,由于这个原因,我的托管服务使用了lite速度服务器,因此我编写了<ifModule mod_rewrite.c><IfModule LiteSpeed>整数),www.example.com/a-1在内部显示www.example.com/a-1.html的内容。链接没有任何变化。

代码语言:javascript
复制
<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-letterb-1 b-2 b-3做了修改,就像上面只有不同的字母。

我在内部用以下代码重写了非/comments/a/链接:

代码语言:javascript
复制
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/comments/a/$1.html -f
RewriteRule ^/?([^.?]+)$/?$ comments/a/$1.html [L,NC]

对于非comments/b/链接,像上面这样的只有字母不同。

按照顺序,我的.htaccess文件的内容如下:

代码语言:javascript
复制
##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##
.
.
.
------------------------------------------------------------------------

每件事都可以用这个代码和我的网站有完全友好的网址。特别感谢@arkascha@anubhava的帮助。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/71336709

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档