user1170540
user1170540

Reputation:

Rewrite rule not working without trailing slash



I have a working rewrite rule to hide index.php?dir= from the URL.
So for instance if I try

www.example.com/folder/dir1/

it rewrites it to

www.example.com/folder/index.php?dir=dir1/

and that fine!

The trouble is if I remove the trailing slash from the URL i.e.

www.example.com/folder/dir1

it goes into a redirection loop!

My complete htaccess is:

Options +FollowSymLinks 

RewriteEngine On
RewriteBase /folder

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]

RewriteCond %{QUERY_STRING}  ^dir=(.*)$ [NC]
RewriteRule ^ %1? [L,R=301,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d

RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

Please advice?

Upvotes: 1

Views: 1928

Answers (2)

user1170540
user1170540

Reputation:

Figured it out!

Had to replace

RewriteRule ^(.+)/? index.php?dir=$1 [L,QSA]

with

RewriteRule ^(.*)? index.php?dir=$1/ [L,QSA]

Thanks everyone for contributing..

Upvotes: 1

TerryE
TerryE

Reputation: 10898

(i) I am confused about the RewriteBase /papers. This only makes sense in DOCROOT/papers/.htaccess. If this the location and is "folder" == papers? If not then, I am not surprised that the rewrite engine is getting confused. (ii) `%{REDIRECT_STATUS} is not 200 on a subquery lookup to evaluate the default if MultiViews or DirectoryIndex is a match.

So before you do anything else:

  • Validate that your base is correct, and if not fix it.
  • Use Options -MultiViews if you don't use them.
  • Check your system, vhost config and DOCROOT/.htaccess to see if a DirectoryIndex is specified. (Unlike rewrite rules which are only taken from the lowest .htaccess, all are scanned for directives such as this.)
  • Replace the RewriteCond %{ENV:REDIRECT_STATUS} 200 by

     RewriteCond %{ENV:REDIRECT_END}%{IS_SUBREQ} true
    

    and add the flag E=END:true to any rules that you want to force to end of the cycle as a match (similar to the Apache 2.4 [END] flag) The extra %{IS_SUBREQ} prevents the rules being fired on a subquery. You don't want this to happen unless you really know what you are doing.

Upvotes: 1

Related Questions