简体   繁体   中英

mod_rewrite - rule according to url path

I am pretty weak at regex, and I need help with a mod_rewrite rule that does the following:

  1. If the url is just the domain name: xyz.com -> go to index.php on directory a like so xyz.com/a/index.php
  2. If the url includes a path: xyz.com/abcde -> go to index.php on directory b and change the path to a param like so: xyz.com/b/index.php?id=abcde

Both directories a and b are under the same root directory of course.

I managed to do number 2, but I have no idea how to include rule 1.

You can use the following :

RewriteEngine on
#--if url is just the domain name--#
#--rewrite to /a/index.php--#
RewriteRule ^$ /a/index.php [NC,L]

#--if it contains path--#
#--rewrite to /b/index.php?id=path
RewriteRule ^((?!b).+)$ /b/index.php?id=$1 [NC,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM