views:

208

answers:

2

Hi 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/sitename/] pass through /var/www/sitename/destination.php&organisation=folder1

A: 

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

Addsy
The second rule will never be used as the first will swallow up everything it can match.
Ignacio Vazquez-Abrams
Query strings are not part of the URL and can't be used in RewriteRule. Also, ? is a regular expression quantifier, meaning "0 or 1 occurrences of the preceding text".
Christian Davén
A: 

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.

Christian Davén