简体   繁体   English

htaccess无法正常工作

[英]htaccess doesn't work as expected

I have an htaccess rewrite URL as below: 我有一个htaccess重写URL,如下所示:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mywebsite.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]

RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f


RewriteRule ^my-page\.html$ /my-page.php [L]
RewriteRule ^my-page/([^/]*)\.html$ /level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*)\.html$ /level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*)\.html$ /level3.php?level3=$1 [L]

These rules above rewrite URLs from mywebsite.com/my-page.php to mywebsite.com/my-page.html . 上面的这些规则将URL从mywebsite.com/my-page.php重写为mywebsite.com/my-page.html

Now, what I want to achieve is mywebsite.com/my-page/ to be redirected to mywebsite.com/my-page.php (which in turn rewrites to mywebsite.com/my-page.html ). 现在,我要实现的是将mywebsite.com/my-page/重定向到mywebsite.com/my-page.php (这又将重写为mywebsite.com/my-page.html )。

What I have tried, I created a directory "my-page" and tried to redirect requests from mywebsite.com/my-page/ to /my-page.html . 我尝试过的工作是创建目录“ my-page”,并尝试将请求从mywebsite.com/my-page/重定向到/my-page.html

I don't know what went wrong. 我不知道出了什么问题。 I can see in the network tab that a request is made to /my-page/ and gets rewritten to mywebsite.com/my-page.htmlmy-page/ , which gives a 302 Status ☹ 我可以在网络标签中看到对/my-page/的请求已被重写为mywebsite.com/my-page.htmlmy-page/ ,状态为302☹

Please help! 请帮忙! Thank you. 谢谢。

You can try use RedirectMatch to achieve this. 您可以尝试使用RedirectMatch实现此目的。

Redirect to my-page.php: 重定向到my-page.php:

RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.php

or straight away to my-page.html if this is your goal: 或直接访问my-page.html(如果这是您的目标):

RedirectMatch 301 ^/my-page/ http://mywebsite.com/my-page.html

or, what will be best - change the code responsible for mywebsite.com/my-page.htmlmy-page/ , but I can't see it in question you have asked :) 或者,最好的方法是-更改负责mywebsite.com/my-page.htmlmy-page/的代码,但我看不到您提出的问题:)

Please give the following a try. 请尝试以下方法。 Brief descriptions are found in the comments for each section. 在每个部分的注释中都有简要说明。

RewriteEngine On

# Trim www.
RewriteCond %{HTTP_HOST} ^www\.mywebsite\.com$ [NC]
RewriteRule ^(.*)$ http://mywebsite.com/$1 [R=301,L]

RewriteBase /

# Redirect /my-page[/] to /my-page.html
# >> Note: change 302 to 301 to make permanent
RewriteRule ^my-page/?$ my-page.html [R=302,L]

# Allow existing files and directories
# Recommended to comment out the first line
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

# Rewrite *.html to respective page
RewriteRule ^my-page.html$ my-page.php [L]
RewriteRule ^my-page/([^/]*).html$ level1.php?num=$1 [L]
RewriteRule ^my-page/([^/]*)/([^/]*).html$ level2.php?level1=$1&level2=$2 [L]
RewriteRule ^([^/]*).html$ level3.php?level3=$1 [L]

The important part here is that you do the required redirect before any other rewrites (except the www. removal). 此处的重要部分是您必须进行必要的重定向,然后再进行其他任何重写( www.删除除外)。

Also, you previously had the two conditions which stated that if the request was not for a file or directory, then proceed with the next rule, but that wouldn't have accounted for the last two rules. 另外,您先前有两个条件,条件是如果请求不是针对文件或目录的,则继续执行下一条规则,但这不会解释最后两个规则。 As such, this version tells Apache to stop everything if the request is for an existing file or directory. 这样,如果请求是针对现有文件或目录的,此版本将告诉Apache停止所有操作。 I would recommend, for security purposes, that you comment out the line that checks for existing directories. 为了安全起见,我建议您注释掉检查现有目录的行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM