How to make dynamic mod-rewrite for subdirs

▼魔方 西西 提交于 2019-12-13 01:39:06

问题


I have this dir structure on my webpage:

rootOfweb/services/oneservice/service1.php
rootOfweb/services/oneservice/service2.php

rootOfweb/services/anothersercice/service1.php
rootOfweb/services/anothersercice/service2.php

www.example.com/index.php?sideID=nameOfService&p=services/oneservice/service2

I have this code in php including files thats needed:

 <?php
     if(isset($_GET["p"]))
     {
      $side = $_GET["p"];
      include ($side.'.php');
     }
 ?>

I have diffrent names on the diffrent subdirs and i have made one mod_rewrite that work on one of the conditions:

RewriteRule ^([a-åA-Å0-9_!-]+)/([a-åA-Å0-9_!-]+)$ index.php?sideID=$1&p=services/oneservice/$2

The aboue rule work for the first service (rootOfweb/services/oneservice/ -all service files -)

I need to create a rule that can work on all subdirs.


回答1:


Following on from from discussion in comments section above you can have rewrite rules like this:

RewriteEngine On
RewriteBase /

RewriteRule ^([\w!-]+)/([\w!-]+/[^/]+/[\w!-]+)/?$ index.php?sideID=$1&p=$2 [L,QSA,NC]

This will support URLs like these:

http://example.com/services/subdir/2014/subfile1
http://example.com/services/subdir/2013/subfile2
http://example.com/services/subdir123/2010/subfile234



回答2:


You should be able to just change your regex:

RewriteRule ^([a-åA-Å0-9_!-]+)/([/a-åA-Å0-9_!-]+)$ index.php?sideID=$1&p=subdir/$2


来源:https://stackoverflow.com/questions/26531221/how-to-make-dynamic-mod-rewrite-for-subdirs

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