简体   繁体   中英

mod_rewrite a double directory

I'm using mod_rewrite in my .htaccess to change a double directory structure into a double GET query string like so:

URL: http://domain.com/test/me/

After mod_rewrite: http://domain.com/index.php?u=test&c=me

using the following code in my .htaccess file:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/$ /index.php?u=$1&c=$2 [L]

That works great, but if a second directory is not specified (eg http://domain.com/test/ ) I want the c variable to equal "all" like so:

http://domain.com/index.php?u=test&c=all

How can I do this? Thanks , regex looks like klingon poetry to me. I've tried a few different variations of the above code with no success.

PS bonus points if you can add a trailing / even if one is not typed into the url box, so that http://domain.com/test/me is handled the same as http://domain.com/test/me/ and http://domain.com/test is treated the same as http://domain.com/test/

Like this?

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\/?$ index.php?u=$1&c=all
RewriteRule ^([^/]*)/([^/]+)\/?$ index.php?u=$1&c=$2 [L]

Also did the bonus :P

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