Nathan Brown
Nathan Brown

Reputation: 67

.htaccess rewrite issue with no slash

I've searched for awhile now and can't find a solution to my issue, maybe I'm just not searching for the right thing, but anyways, here's what's happening.

I have the following mod_rewrites in my .htaccess file. The rewrites work perfectly, but if I do NOT have the trailing slash, then it show's a variable automatically in the url. Please see the example below to get a clear understanding.

URL Sample

If I enter http://website.com/test/, (notice the trailing slash) the page URL will stay the exact same and load the content perfectly!

BUT, if I forget the trailing slash, ie: http://website.com/test, then the page URL will change to http://website.com/test/?var1=test. (now notice I had left out the trailing slash to begin with) the content still loads properly and everything works perfectly fine, just my URL is "ugly" now.

I hope I made this clear, but if you have any questions, please feel free to ask. Thank you!

.htaccess Code

    Options +FollowSymLinks
    RewriteEngine On

    RewriteCond %{HTTPS} off
    RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]

    RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L]
    RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4 [L]
    RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3 [L]
    RewriteRule ^([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2 [L]
    RewriteRule ^([\w-]+)/?$ _client.php?var1=$1 [L]

Upvotes: 2

Views: 466

Answers (1)

anubhava
anubhava

Reputation: 785058

This is happening due to mod_dir module that adds a trailing slash after your mod_rewrite rules have finished. Have your rules like this to avoid this problem:

DirectorySlash Off
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]

# add a trailing slash to directories
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L,R=302]

RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4&var5=$5 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3&var4=$4 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2&var3=$3 [L,QSA]
RewriteRule ^([\w-]+)/([\w-]+)/?$ _client.php?var1=$1&var2=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ _client.php?var1=$1 [L,QSA]

Upvotes: 2

Related Questions