为什么我的 .htaccess 301 重定向规则不起作用?

发布于 2024-12-02 07:41:36 字数 2043 浏览 0 评论 0原文

我在重新设计的网站上使用 URL 重写来为我的页面提供更整洁的 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 错误,或者如果我使用 Redirect 301 /about-page.html /about ,它似乎可以正常工作,但不幸的是旧 URL 使用 .php 扩展名,而不是 .html 扩展名

。相信问题与其他规则之一有关,它将 /xyz 的请求重定向到 /xyz.php,可能会造成一些无限循环,但我不知道如何解决它,

非常感谢您


编辑:最后,正在工作 。 .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]

I use URL rewriting on my redesigned website to give my pages tidier URLs.

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]

This .htaccess file allows /filename to actually point to /filename.php. It all works fine.

However, I have now realised that I should set up 301 permanent redirects, so that the pages of the old website (before the redesign) can redirect to pages on the new site (for SEO and linking reasons). The pages have been reorganised, so multiple old pages will redirect to new pages, for example.

The old website did not use URL rewriting. Therefore, I want to create permanent redirects such as /about-page.php to /about, doing them manually with one rule per old page.

I have tried several things, such as...

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

...or...

Redirect 301 /about-page.php /about

...but it always ends up either not working at all (giving me a 404 error when I attempt to access /old-filename.php, or breaks everything with internal server errors. It seems to work fine if I use Redirect 301 /about-page.html /about instead, but unfortunately the old URLs used .php extensions, not .html extensions.

I believe the problem is related to one of the other rules, which redirect requests for /xyz to /xyz.php, possibly creating some endless loop. But I can't figure out how to fix it.

Any advice? Thank you very much.


Edit: Final, working .htaccess file:

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]

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

属性 2024-12-09 07:41:37
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]

这应该有效,如果出现问题请评论

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]

This should work, comment if problem occurs

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文