301 重定向未按预期工作

发布于 2025-01-05 17:00:17 字数 1761 浏览 0 评论 0原文

尝试在 .htaccess 文件中设置 301 重定向,这就是我正在尝试做的事情,

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

我正在使用 WAMPP 服务器在本地计算机上测试它。尽管当我点击 http://localhost/ wordpress/ 我被重定向到 http://www.mysite.com/wordpress/ 但对于其他 URL,我根本没有被重定向。例如,

我的 URL 中有这个 URL本地机器http://localhost/wordpress/2010/11/shadows/ 和服务器上的 http://www.mysite.com/wordpress/2010/11/shadows/ > 但是当我点击这个 URL 时,我没有被重定向到实时服务器上受尊重的 URL,但我从本地计算机上看到了相同的页面。

工作

http://localhost/wordpress/
=> Redirected to:
http://www.mysite.com/wordpress/

不工作

http://localhost/wordpress/2010/11/shadows/
=> Redirected to:
http://www.mysite.com/wordpress/2010/11/shadows/  

从 URL 可以清楚地看出,我正在尝试在 Wordpress 中执行此操作。 这是完整的 .htaccess 文件

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

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

谁能告诉我重定向条目有什么问题吗?提前致谢

更新 我什至尝试过这个选项

Options +FollowSymLinks 
RewriteEngine on
RewriteBase /wordpress/
RewriteRule ^(.*)$ http://www.mysite.com/wordpress/$1 [L,R=301]

但没有成功。

Trying to set 301 redirect in .htaccess file and here is what i am trying to do

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

I am testing this on my local machine using WAMPP server.Though when i hit http://localhost/wordpress/ i am getting redirected to http://www.mysite.com/wordpress/ but for other URL i am not getting redirected at all.for e.g

I have this URL in my local machine http://localhost/wordpress/2010/11/shadows/ and this at the server http://www.mysite.com/wordpress/2010/11/shadows/ but when i hit this URL i am not getting redirected to respected URL on the live server ,but i am being showed same page from local machine.

Working:

http://localhost/wordpress/
=> Redirected to:
http://www.mysite.com/wordpress/

Not working

http://localhost/wordpress/2010/11/shadows/
=> Redirected to:
http://www.mysite.com/wordpress/2010/11/shadows/  

As clear from URL, I am trying to do this in Wordpress.
Here is complete .htaccess file

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /wordpress/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>

# END WordPress

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

Can any one tell me whats wrong with the redirection entry? Thanks in advance

Update
I have even tried this option

Options +FollowSymLinks 
RewriteEngine on
RewriteBase /wordpress/
RewriteRule ^(.*)$ http://www.mysite.com/wordpress/$1 [L,R=301]

Did not worked.

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

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

发布评论

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

评论(2

亢潮 2025-01-12 17:00:17

始终是您先于其他人谈论 RewriteRules,伙计!:D)(而且您已经忘记了 QSA 指令)。

因此,这是 RewriteRule 的“干净”版本:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # BEGIN My Own rewrite rules
  RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
  RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
  RewriteRule (.*) http://www.mysite.com/wordpress/$1 [QSA,R=301,L]
  # END My Own rewrite rules

  # BEGIN WordPress
  RewriteBase /wordpress/
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /wordpress/index.php [L]
  # END WordPress

</IfModule>

如果它位于 .htaccess 文件中,则尝试不使用 /,如下所示:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # BEGIN My Own rewrite rules
  RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
  RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
  # Without the / after wordpress:
  RewriteRule (.*) http://www.mysite.com/wordpress$1 [QSA,R=301,L]
  # END My Own rewrite rules

  # BEGIN WordPress
  RewriteBase /wordpress/
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /wordpress/index.php [L]
  # END WordPress

</IfModule>

顺便说一句,这是许多人中的第一次年,找到一个“正在建设”页面真好!


请告诉我是否有效。

Always you first then the others (talking about RewriteRules only, man! :D ) (and you've forgotten the QSA directive).

So here's the "clean" version of your RewriteRule:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # BEGIN My Own rewrite rules
  RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
  RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
  RewriteRule (.*) http://www.mysite.com/wordpress/$1 [QSA,R=301,L]
  # END My Own rewrite rules

  # BEGIN WordPress
  RewriteBase /wordpress/
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /wordpress/index.php [L]
  # END WordPress

</IfModule>

If it's in a .htaccess file then try without the / like this:

<IfModule mod_rewrite.c>
  RewriteEngine On

  # BEGIN My Own rewrite rules
  RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
  RewriteCond %{HTTP_HOST} ^localhost/wordpress/$
  # Without the / after wordpress:
  RewriteRule (.*) http://www.mysite.com/wordpress$1 [QSA,R=301,L]
  # END My Own rewrite rules

  # BEGIN WordPress
  RewriteBase /wordpress/
  RewriteRule ^index\.php$ - [L]
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule . /wordpress/index.php [L]
  # END WordPress

</IfModule>

By the way this is the first time in many years that a find an "under construction" page nice!


Please tell me if it works.

她比我温柔 2025-01-12 17:00:17

%{HTTP_HOST] 将包含类似 localhostwww.thecolorsofmysoul.com 的内容。所以你的条件永远不会匹配。

RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$

并且重定向到外部域永远不会触发。

另外,前两条规则共同作用,将任何非文件/目录映射到 index.phphttp://localhost/wordpress/ 的正则表达式匹配字符串为“”,因此模式“.”将失败,并且将失败并使用您的“更新”进行重定向。 顺便说一句,尝试

Options +FollowSymLinks 
RewriteEngine on
RewriteBase /wordpress/

RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^.*   http://www.thecolorsofmysoul.com/wordpress/$0 [L,R=301]

使用此基础,这应该位于 DOCROOT/.htaccess 中。相应的 WordPress 规则(不应在此重定向之上)是

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$)    index.php   [L]

You don't need to Repeat the base in the target,并且否定先行断言消除了对第一个 index.php 规则的需要。

%{HTTP_HOST] will contain something like localhost or www.thecolorsofmysoul.com. So your conditions will never match.

RewriteCond %{HTTP_HOST} ^http://localhost/wordpress/$ [OR]
RewriteCond %{HTTP_HOST} ^localhost/wordpress/$

and the redirecting to the external domain will never fire.

Also the first two rule act in consort to map any non-file/directory to index.php. http://localhost/wordpress/ has a regexp match string of "" so will fail the pattern "." and will fail through and will redirect with your "update". Try

Options +FollowSymLinks 
RewriteEngine on
RewriteBase /wordpress/

RewriteCond %{HTTP_HOST} =localhost
RewriteRule ^.*   http://www.thecolorsofmysoul.com/wordpress/$0 [L,R=301]

BTW with this base this should be in DOCROOT/.htaccess. The corresponding Wordpress rules (which shouldn't be above this redirect) are

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(?!index\.php$)    index.php   [L]

You don't need to repeat the base in the target and the negative lookahead assertion removes the need for the first index.php rule.

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