【问题标题】:.htaccess extensionless url and 301 redirect loop.htaccess 无扩展 url 和 301 重定向循环
【发布时间】:2014-03-26 21:44:02
【问题描述】:

我花费了无数个小时来尝试开发一个好的 .htaccess 来为 SEO 目的提供无扩展名的 url。我看到的几乎每一个现代网站都有无扩展名的 URL,作为一个相当新的网站,我想采用这种方法。

我已经能够使用 .htaccess 成功地将网站转换为无扩展名。例如:

example.com/page.php 将成为 URL 中的 example.com/page1

但是,相同位置的现有页面(例如 contact.php 页面)在执行 301 重定向时会执行 LOOP。因此,例如,为了让搜索引擎不认为 /contact.php 和 /contact 是两个不同的页面,现有的索引 /contact.php 有一个 301 重定向到 /contact

但是,/contact url 在后台从 /contact.php 提取数据,.htaccess 知道这一点,然后想再次将 301 重定向应用到后台 /contact.php 到 /contact 导致循环。我不知道如何阻止这种情况。

底线是我想要一个从 /contact.php 到 /contact 的 301 重定向,并且 URL 中的 /contact 反映了 /contact.php 物理文件的内容。

这是我的 .htaccess,除了具有 301 重定向的页面外,它运行良好。

我也对您在我的 .htaccess 中可能看到的任何其他问题感兴趣 :)

.htaccess
    Options -MultiViews
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /


    ## 301 Redirects
        # 301 Redirect 1
        RewriteCond %{QUERY_STRING}  ^$
        RewriteRule ^contact\.php$ /contact? [R=301,NE,NC,L]


    ## Exclude appropriate directories from rewrite rules  ^(blogsphere)
        RewriteRule ^(blogsphere) - [L]

    ## Unless directory, remove trailing slash
        # If requested URL ending with slash does not resolve to an existing directory
            RewriteCond %{REQUEST_FILENAME} !-d
        # Externally redirect to remove trailing slash
            RewriteRule ^(.+)/$ $1 [QSA,L]

    ## Redirect external .php requests to extensionless url
        # Any type of POST/GET should be a condition so that it doesnt rewrite without that data (end fileames in submissionscript)
            RewriteCond %{REQUEST_URI} !^/([a-z-]+)\-(submissionscript)(.*)$
        #Request has to end in .php
            RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
            RewriteRule ^(.+)\.php$ $1$2 [QSA,L]

    ## If it is not a directory then internal resolve request to end with .php for extensionless php urls (/page will internally resolve to /page.php)
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteRule ^((?!(\.|\.php)).)*$ $0.php [QSA,L]

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    像这样:

    Options -MultiViews
    Options +FollowSymLinks
    RewriteEngine on
    RewriteBase /
    
    # skip blogsphere directory for rewrite
    RewriteRule ^(blogsphere) - [L]
    
    ## Unless directory, remove trailing slash
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
    RewriteRule ^(.+?)/$ $1 [R=301,L]
    
    # remove .php extension from URL - external redirect 
    RewriteCond %{REQUEST_URI} !^/([a-z-]+)-(submissionscript)(.*)$
    RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
    RewriteRule ^ %1 [R=301,L,NE]
    
    # internally rewrite a file to file.php
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
    RewriteRule ^(.+?)/?$ $1.php [L]
    

    【讨论】:

    • 您可以评论简单的行项目以供我和其他人学习吗?
    猜你喜欢
    • 2016-09-28
    • 2013-01-27
    • 1970-01-01
    • 2013-12-21
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多