mod_rewrite convert certain folders to parameters
I have a site where I'd like to convert the first subfolder into a parameter (from a set list of subfolders), so
http://localhost/mysite/folder1/dosomething.php
is displayed as shown above, but to PHP it looks like:
... localhost/mysite/dosomething.php?organisation=folder1
I only need to do this on certain folders (i.e. not ... localhost/mysite/admin/ for example) and I need to take into account the fact that there may or may not already be parameters appended to the URL
I'm creating a site for multiple organisations to use, and based on the organisation, colours etc in the site will need to be different, but each organisation will be using the same pages, and the client wants the organisation shortcode in the URL. I thought the best way to do this would be with mod_rewrite.
I've tried:
rewriterule ^folder1/(.*) $1&organisation=folder1 [NC]
but this doesnt work or handle the fact there might already be parameters attached to the URL. Can anyone suggest a way to do this?
Kind regards and many thanks
Based on your feedback:
Hmm I can't get this to work. In .htaccess I have Options +FollowSymLinks RewriteEngine on RewriteRule ^folder1/(.*) $1&organisation=folder1 [NC,QSA]
If I browse ... server/sitename/folder1/destination.php I get a page not found error, and the htaccess debug log says:
(3) [perdir /var/www/sitename/] add path info postfix: /var/www/sitename/folder1 -> /var/www/sitename/folder1/destination.php (3) [perdir /var/www/sitename/] strip per-dir prefix: /var/www/sitename/folder1/destination.php -> folder1/destination.php (3) [perdir /var/www/sitename/] applying pattern '^folder1/(.)' to uri 'folder1/destination.php' (2) [perdir /var/www/sitename/] rewrite 'folder1/destination.php' -> 'destination.php&organisation=folder1' (3) [perdir /var/www/sitename/] add per-dir prefix: destination.php&organisation=folder1 -> /var/www/sitename/destination.php&organisation=folder1 (2) [perdir /var/www/sitename/] strip document_root prefix: /var/www/sitename/destination.php&organisation=folder1 -> /sitename/destination.php&organisation=folder1 (1) [perdir /var/www/sitename/] internal redirect with /sitename/destination.php&organisation=folder1 [INTERNAL REDIRECT] (3) [perdir /var/www/sitename/] strip per-dir prefix: /var/www/sitename/destination.php&organisation=folder1 -> destination.php&organisation=folder1 (3) [perdir /var/www/sitename/] applying pattern '^folder1/(.)' to uri 'destination.php&organisation=folder1' (1) [perdir /var/www开发者_StackOverflow社区/sitename/] pass through /var/www/sitename/destination.php&organisation=folder1
To append parameters to the query string (instead of replacing them) use the QSA flag:
RewriteRule ^folder1/(.*) $1&organisation=folder1 [NC,QSA]
"This flag forces the rewrite engine to append a query string part of the substitution string to the existing string, instead of replacing it. Use this when you want to add more data to the query string via a rewrite rule."
See Apache documentation for mod_rewrite.
The correct rules would be:
# rewrite for php scripts
RewriteRule ^(folder[0-9]+)(/([^/]*\.php)?)$ .$2?organisation=$1 [NC,QSA]
# rewrite for css and js (assuming they are relative to path of your htaccess)
RewriteRule ^(folder[0-9]+)/(.*) ./$2 [NC,QSA]
Though this should work too if you don't mind sending your param everywhere:
RewriteRule ^(folder[0-9]+)/(.*) $2?organisation=$1 [NC,QSA]
I would suggest
RewriteRule ^folder1/(.*)?(.*) $1?organisation=folder1&$2 [NC]
RewriteRule ^folder1/(.*) $1?organisation=folder1 [NC]
I suspect someone who is a bit niftier with RegExs could combine these into one rule tho
Also make sure you have mod rewrite turned on by having
RewriteEngine on
above the rewrite rules in your .htaccess
精彩评论