【问题标题】:htaccess Re-writing URL with redirect causes loophtaccess 使用重定向重写 URL 导致循环
【发布时间】:2015-04-17 00:18:21
【问题描述】:

我有一个现有的站点,其 URL 中有 php 被编入索引。我想将 URL 更改为不包含 PHP,因此 page.php 将被重写为 http://example.com/thepage

重写工作正常,但是当我尝试使用 page.php 访问页面时,重定向会导致“页面无法在循环中显示”错误

我需要弄清楚如何设置重定向并重写 URL,因为 Google 现在已将 PHP URL 和漂亮 URL 编入索引。

我在网上看了无数页,但找不到答案。

请帮忙。

这是我的 htaccess

# The redirect below caused a loop when I access the page as 
# http://example.com/thepage.php
# Moving the redirect after the rewrite didn't work either. 

Redirect 301 /thepage.php http://example.com/thepage

RewriteEngine on
# Rewrite works. The page does display corectly for this link
#    http://example.com/thepage

<IfModule mod_rewrite.c> 
   Options +FollowSymLinks 
   Options +Indexes 
 #  RewriteEngine On 
   RewriteCond %{SCRIPT_FILENAME} !-d 
#RewriteRule ^photos([^\.]+)$ photos [NC,L]
   RewriteRule ^([^\.]+)$ $1.php [NC,L] 
</IfModule>


ErrorDocument 404 http://example.com/

谢谢

【问题讨论】:

    标签: .htaccess mod-rewrite redirect


    【解决方案1】:

    你的重写规则听起来是倒退的。

    在这里,我们捕获所有不包含句点的 php 文件,并将它们重定向到仅使用名称而不使用扩展名的目录 URI。 [R=301] 是一个永久的 301 重定向,它告诉任何人该页面不再位于其旧位置。

    RewriteEngine on
    RewriteRule ^([^\.]+).php$ $1 [NC,L,R=301] 
    

    您可能只想在不使用 301 的情况下使用 R 测试这些以确保其正常工作。如果您执行的 301 重定向不起作用,它将被缓存在最终用户的浏览器缓存中,直到过期。

    Apache 有一个extensive reference on remapping URLs with redirects

    【讨论】:

    • 谢谢。当我这样做时,除了 Options +FollowSymLinks Options +Indexes RewriteCond %{SCRIPT_FILENAME} !-d RewriteRule ^([^\.]+).php$ 上的主页 RewriteEngine 之外的所有页面都出现 404 错误$1 [NC,L,R=301] #ErrorDocument 404 example.com
    【解决方案2】:

    我终于能够回到这个问题并找到解决方案。我希望这可以帮助别人。我在这个问题上大发雷霆。

    我找到了很多例子,但没有我需要的组合

    #Force non-www:
    RewriteEngine on
    RewriteCond %{HTTP_HOST} ^www.example.com [NC]
    RewriteRule ^(.*)$ http://example.com/$1 [L,R=301,NC]
    
    RewriteBase /
    
    # hide .php extension
    # To externally redirect /dir/foo.php to /dir/foo
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).php [NC]
    RewriteRule ^ %1 [R,L,NC]
    
    # To internally redirect /dir/foo to /dir/foo.php
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [L]
    
    ErrorDocument 404 http://example.com/
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-09-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-02-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多