I found a Question similar to my problem(Regular Expression For URL Query) but could not make my problem to be resolved.
So I am having trouble with Regular Expressions in HTACCESS.
So what I have in my website is :
ROOT : (my web root is in a sub folder ‘sites’)
/sites/index.php and other files
/sites/sites/sample_folder_name/
Now what I would like to do is to ReWrite URL :
/sites/sites/sample_folder_name/file_name.php
to :
/sites/file_name.php?siteredirect=sample_folder_name
Remember :
If the URL contains any Request Parameters, then those must also be rewrited, i.e :
Example :
/sites/sites/sample_folder_name/file_name.php?cat_id=1&user=2
to :
/sites/file_name.php?cat_id=1&user=2&siteredirect=sample_folder_name
The parameters are supposed to be unknown instead of user and cat_id. It could be any parameters. Means that whatever is written after .php should also come in URL.
Means :
/sites/sites/sample_folder_name/file_name.php?if_any_parameters
then :
/sites/file_name.php?if_any_parameters&siteredirect=sample_folder_name
otherwise :
/sites/file_name.php?siteredirect=sample_folder_name
Currently, the rewrite is working great but I am not able to make them work with parameters.
What I have in my HTACCESS right now :
RewriteRule ^sites\/([0-9A-Za-z_]+)\/([0-9A-Za-z_]+)\.php(.*)$ $2.php(?(?=$3)$3&)siteredirect=$1 [L]
RewriteRule ^sites\/([0-9A-Za-z_]+)\/$ index.php?siteredirect=$1 [L]
After this, it is giving me a 404.
Please Help… Where am I doing it wrong !
You need the
QSAflag (Query String Append):What’s more, invert your rules: you won’t have to use
LAlso, consider using
<Location>directives, it will make your job easier: