htaccess使用多个正斜杠重写URL

时间:2015-01-21 03:46:27

标签: php apache .htaccess mod-rewrite

尝试制定一组将设置这些链接的.htaccess mod_rewrite规则:

domain.com/content-page
domain.com/region/another-page
domain.com/region/brand/more-content-here

获取文件:

domain.com/content-page.php
domain.com/region-another-page.php
domain.com/region-brand-more-content-here.php

'region'和'brand'以及页面名称都是可变/可更改的。

到目前为止,我有:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,C]
RewriteRule ([^/]+)/([^/]+) $1-$2 [NC,L] 

这只适用于一个正斜杠。如上所述,域之后的URL可能没有,一个或两个正斜杠。没有任何运气试图扩展这个来处理多个(或没有)斜杠。

1 个答案:

答案 0 :(得分:3)

尝试:

RewriteEngine On

# pass through root
RewriteRUle ^(index\.php)?$ - [L]

# no more / so add extension
RewriteCond $1 !/
RewriteCond $1 !\.php$
RewriteCond ${REQUEST_FILEAME} !-f
RewriteRule ^(.*)$ /$1.php [L]

RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)/(.*)$ /$1-$2 [L]