I am new to mod_rewrite and I have this problem:
I have a working redirect with mod_rewrite, my .htaccess:
RewriteEngine on RewriteRule ^microsite/([^/\.]+)/$ micrositecontroller.php?name=$1 [L]
micrositecontroller.php only echoes a text
On the browser when I enter my URL:
I am redirected to where wanted but when I enter:
It still redirects to where wanted but the URL becomes like this:
Now what I want is that trailing "/?name=test" not to show up.
I tried different combinations of the regex but to no avail and I have no idea if it is normal of not. Any idea?
You have a slash in your regex, so your regex handles slash... just remove it:
RewriteRule ^microsite/([^/\.]+)$ micrositecontroller.php?name=$1 [L]
What you want is:
RewriteRule ^microsite/([^/\.]+)/?$ micrositecontroller.php?name=$1 [L]
# Note the "?"-------------------^
To accommodate both ending with a slash and no slash. The problem with leaving the slash on (or off) is apache forces a browser redirect, thus making the
?name=test show up in the browser's location bar.
This is because mod_dir and the
DirectorySlash on directive is interferring. The DirectorySlash directive tells apache to redirect the browser when it accesses what looks to be a directory (in your case
localhost/project/microsite/test to the smae URI except with a trailing slash.