【问题标题】:How the rewrite in clean url structure with htaccess如何使用 htaccess 重写干净的 url 结构
【发布时间】:2014-03-01 12:37:31
【问题描述】:

我已更改网站的结构:删除 /nl/ 文件夹并将下划线切换为条纹。 所以希望是重定向旧页面: www.example.com/nl/citygames_leuven 到 http://www.example.com/citygames-leuven

重定向有点作用;但是我的干净 URL 重定向出现问题。 我被重定向到: http://www.example.com/citygames-leuven?url=nl/citygames_leuven

如你所见; url 参数粘在页面上并给出错误。

这是我目前的代码:

Options All -Indexes 
RewriteEngine On 
RewriteBase / 


RewriteRule ^teambuilding/(.*)/?$ teambuilding.php?url=$1 [L]
RewriteRule ^citygame/(.*)/?$ citygame.php?url=$1 [L]
RewriteRule ^meeting/(.*)/?$ meeting.php?url=$1 [L]
RewriteRule ^aanzee/(.*)/?$ aanzee.php?url=$1 [L]
RewriteRule ^vip/(.*)/?$ vip.php?url=$1 [L]
RewriteRule ^verhuur/(.*)/?$ verhuur.php?url=$1 [L]
RewriteRule ^nieuws/(.*)/?$ nieuws.php?url=$1 [L]


#Url omzetten naar index.php?page= 
RewriteCond %{REQUEST_FILENAME} !-f [NC] 
RewriteCond %{REQUEST_FILENAME} !-d [NC] 
RewriteRule ^(.+)$ page.php?url=$1 [QSA] 



Redirect 301 /nl/citygames_antwerpen http://www.example.com/citygames-antwerpen
Redirect 301 /nl/citygames_leuven http://www.example.com/citygames-leuven
Redirect 301 /nl/teambuilding/offroad_buggy http://www.example.com/teambuilding/offroad-buggy

【问题讨论】:

    标签: php .htaccess mod-rewrite redirect url-rewriting


    【解决方案1】:

    重新排序您的规则并将 301 放在内部重写规则之前:

    Options All -Indexes 
    RewriteEngine On 
    RewriteBase / 
    
    RewriteRule ^nl/citygames_antwerpen/?$ /citygames-antwerpen [L,R=301]
    RewriteRule ^nl/citygames_leuven/?$ /citygames-leuven [L,R=301]
    RewriteRule ^nl/teambuilding/offroad_buggy/?$ /teambuilding/offroad-buggy [L,R=301]
    
    RewriteRule ^teambuilding/(.*)/?$ teambuilding.php?url=$1 [L]
    RewriteRule ^citygame/(.*)/?$ citygame.php?url=$1 [L]
    RewriteRule ^meeting/(.*)/?$ meeting.php?url=$1 [L]
    RewriteRule ^aanzee/(.*)/?$ aanzee.php?url=$1 [L]
    RewriteRule ^vip/(.*)/?$ vip.php?url=$1 [L]
    RewriteRule ^verhuur/(.*)/?$ verhuur.php?url=$1 [L]
    RewriteRule ^nieuws/(.*)/?$ nieuws.php?url=$1 [L]
    
    #Url omzetten naar index.php?page= 
    RewriteCond %{REQUEST_FILENAME} !-f [NC] 
    RewriteCond %{REQUEST_FILENAME} !-d [NC] 
    RewriteRule ^(.+)$ page.php?url=$1 [L,QSA] 
    

    PS:最好在 mod_rewrite 本身中执行此操作并避免 mod_alias 规则。

    【讨论】:

      猜你喜欢
      • 2016-04-24
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-09-22
      • 1970-01-01
      • 2023-03-30
      相关资源
      最近更新 更多