htaccess rewriting URLs with multiple forward slashes

眉间皱痕 提交于 2019-12-12 19:18:21

问题


Trying to work out a set of .htaccess mod_rewrite rules that will set these links:

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

to fetch files:

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

Where 'region' and 'brand' and the page name are all variable/changeable.

So far I have:

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

This works but only for one forward slash. As mentioned above, URLs after the domain may have none, one or two forward slashes. Haven't had any luck trying to extend this to deal with multiple (or not) slashes.


回答1:


Try:

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]


来源:https://stackoverflow.com/questions/28059280/htaccess-rewriting-urls-with-multiple-forward-slashes

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!