Issue
This isn't so much a question looking for a solution, rather a question looking for some clarification.
A couple of days ago I was having a ton of trouble getting my mod_rewrites that worked fine on localhost (running XAMPP) to work on my production server (CentOs 6 LAMP stack).
The rewrite looked like this:
RewriteEngine On
RewriteBase /
RewriteRule ^my/page/([a-zA-Z0-9]+)$ /my/page/$1/
RewriteRule ^my/page/([a-zA-Z0-9]+)/$ /my_page.php?t=$1
On localhost, the above line worked perfectly and performed the following:
my/page/ab123 --> my_page?t=123
However, on my production server the above rule was having no effect. I'm extremely new to mod_rewrites and server architecture in general so finding the solution took me hours and hours. Now, I'd love to know why the below fixed my issue because I've honestly got no clue.
RewriteEngine On
RewriteBase /
RewriteRule ^/my/page/([a-zA-Z0-9]+)$ /my/page/$1/
RewriteRule ^/my/page/([a-zA-Z0-9]+)/$ /my_page.php?t=$1
It was a simple change. I had to add a forward slash before the page's name: ^/my/page
I would like to be able to move my htaccess files to my server without always having to add this extra character. Is there a way around this?
Also, the following options appear in my httpd.conf
file
<Directory /my/sites/directory>
Options -Indexes +Multiviews +FollowSymLinks
DirectoryIndex index.php index.html
AllowOverride All
Allow from all
</Directory>
Solution
The leading slash is needed or not needed depending on the context of the rules. If the rules are in an htaccess file (or a per directory context), then the leading slash isn't needed. If the rules are in the server/vhost config, then you need a leading slash. If the version of apache is lower than v2.2 (or 2.0, can't remember), then you always need the leading slash.
It's probably better if you just do this:
RewriteEngine On
RewriteBase /
RewriteRule ^/?my/page/([a-zA-Z0-9]+)/?$ /my_page.php?t=$1
That'll make the leading and trailing slashes optional.
Answered By - Jon Lin Answer Checked By - Clifford M. (WPSolving Volunteer)