Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/.htaccess/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
为什么不是';我的.htaccess 301重定向规则是否正常工作?_.htaccess_Redirect_Http Status Code 301 - Fatal编程技术网

为什么不是';我的.htaccess 301重定向规则是否正常工作?

为什么不是';我的.htaccess 301重定向规则是否正常工作?,.htaccess,redirect,http-status-code-301,.htaccess,Redirect,Http Status Code 301,我在重新设计的网站上使用URL重写,使我的页面更加整洁 RewriteEngine On RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^$ index.php [L] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^.+/$ %{REQUEST_URI}index.php [L] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^

我在重新设计的网站上使用URL重写,使我的页面更加整洁

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^$ index.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^.+/$ %{REQUEST_URI}index.php [L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
这个.htaccess文件允许/filename实际指向/filename.php。一切都很好

然而,我现在意识到我应该设置301个永久重定向,以便旧网站的页面(在重新设计之前)可以重定向到新网站的页面(出于SEO和链接原因)。这些页面已被重新组织,例如,多个旧页面将重定向到新页面

旧网站没有使用URL重写。因此,我想创建永久重定向,例如/about-page.php到/about,在每个旧页面上手动执行这些重定向

我试过几种方法,比如

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ about [R=301,L]
……或者

Redirect 301 /about-page.php /about
…但它最终要么根本不起作用(当我尝试访问/old-filename.php时会出现404错误,要么会因内部服务器错误而中断所有内容。如果我使用
重定向301/about-page.html/about
,它似乎可以正常工作,但不幸的是,旧URL使用的是.php扩展名,而不是.html扩展名

我相信这个问题与另一个规则有关,它将对/xyz的请求重定向到/xyz.php,可能会产生一些无休止的循环。但我不知道如何解决这个问题

有什么建议吗?非常感谢


编辑:Final,working.htaccess文件:

DirectoryIndex index.php #

RewriteEngine On

# -- Use a permanent redirect to point the old page to the new page.
# -- The RewriteCond is needed, or a redirect loop happens in certain cases.
# -- The full URL seems to be needed, or it redirects incorrectly.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^about-page.php$ http://www.mywebsite.com/about [R=301,L]

# -- Redirect most other .php files to a 404 error, to avoid duplicate content.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]

# -- Redirect requests without an extension, but for a valid file, to that file.
# -- I'm not sure what the RewriteCond lines are for, but they both seem necessary.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

这应该可以,如果出现问题,请发表评论

感谢您的回复。不幸的是,这似乎不起作用。这会导致“重定向到”,从而导致404错误。将PHP文件重定向到404错误的规则旨在通过强制用户访问重写URL的页面来防止不同URL上的重复内容(没有.php扩展名);我不想让他们直接访问真实的.php文件。如果301重定向发生时可以忽略该规则,我想这会很好。好的,我成功了。我遇到了很多麻烦,因为我的浏览器似乎正在缓存301重定向,即使我从.htacces中删除了该规则,也会继续重定向s文件!我似乎还需要ENV:REDIRECT\u STATUS RewriteCond行,尽管我不确定它的用途。我将编辑我的问题以包含最终结果。感谢帮助。
RewriteRule^about page.php$/about[R=301,L]
引导/阻止它重定向mywebsite.com/home/mysite/mywebsite.com/about您可以选择将所有PHP文件重定向到非PHP地址,但将它们重定向到404错误是不好的。301重定向会被缓存,因此,在您没有完成htaccess文件之前,请使用302重定向,它不会被缓存。
DirectoryIndex index.php # -- this sets index.php to be default file for a folder

RewriteEngine On

# -- RewriteRule ^(.+\.php|(.+/)?index)$ - [R=404,L]
# -- dude this above line redirects all php files to 404 error
# -- so delete this, its a problem not solution

RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]

RewriteRule ^about-page.php$ /about [R=301,L]