【问题标题】:Issue With Redirecting & Query Strings重定向和查询字符串的问题
【发布时间】:2014-07-30 11:17:41
【问题描述】:

在我们的 CMS 上,我们使用以下 .htaccess 代码从我们的 CMS 提供页面:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/?$ index.php?slug=$1

所以页面是通过将页面的 slug 传递给 index.php 来提供的

我们遇到的问题是,例如,如果您转到此页面:

http://www.example.com/index.php?slug=contact

你会被重定向到这个页面:

http://www.example.com/?slug=contact

我们的 .htaccess 中也有这个,可以将 index.php 发送回根目录:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index.php
RewriteRule ^index.php$ http://www.example.com/ [R=301,L]

我们可以做些什么来结束这个 URL:

http://www.example.com/contact

非常感谢任何帮助。

【问题讨论】:

    标签: apache .htaccess mod-rewrite redirect


    【解决方案1】:

    您可以在 root .htaccess 中使用此代码:

    RewriteEngine On
    RewriteBase /
    
    # external redirect from actual URL to pretty one
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(?:index\.php)?\?slug=([^\s&]+) [NC]
    RewriteRule ^ /%1? [R=302,L]
    
    RewriteCond %{THE_REQUEST} /index\.php [NC]
    RewriteRule ^(.*?)index\.php$ /$1 [L,R=302,NC,NE]
    
    # internal forward from pretty URL to actual one
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/([^/]+)/?$ index.php?slug=$1 [L,QSA]
    

    【讨论】:

      猜你喜欢
      • 2011-01-30
      • 1970-01-01
      • 2016-03-17
      • 2011-12-12
      • 2011-02-17
      • 1970-01-01
      • 2015-03-31
      • 1970-01-01
      相关资源
      最近更新 更多