views:

33

answers:

2

I am using a .htaccess file to direct requests for a directory to a custom php file that provides info about that directory (and i want the url displayed by the browser to not change).

Here is the relevant part of the .htaccess file.

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L] 

This works well if you go to a directory and include a trailing slash:

http://domain.com/path/

But without the trailing slash, it doesn't

http://domain.com/path

The url (shown in the browser) turns into:

http://localhost:8888/path/?url=/path

I've tried fixing this by adding a rule above this rule:

RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

But that isn't working for me.

How can I get .htaccess to add the trailing slash if it is omitted and then act just as if it had been there?

Thank you

Update: As requested, here is the whole thing.

<IfModule mod_rewrite.c>
RewriteEngine On

#force trailing slashes on real directories
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]

#use the directory viewer on directories without an index page
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ /myphp.php?url=%{REQUEST_URI} [QSA,L]  

</IfModule>
A: 

Did you mean

RewriteRule ^(.*)$ $1/ [L]

instead?

I don't know about your approach. I would try something like

RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

I've used this for rewriting before and it works:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com
RewriteRule ^(.*)$ http://www.domain.com$1
RewriteRule ^/(\S+)/?$ /myphp.php?url=$1

Note that I didn't include the trailing slash in the first rule.

Also, I would advice you not to rely on .htaccess unless absolutely necessary. This is because the server will check for .htaccess files constantly. Using the httpd.conf file instead means apache will only load conditions and rules once. At least I was adviced to do so because of this.

misterte
You are right, that should have been a *, not a ?. Fixed that on the question. I tried using your suggestion, but have the same effect. To be clear in the version in my question and in the version, the redirect is going to the correct place. The problem is the text displayed by the browser. I don't want the user to see the query string. I just want them to see the path to the directory, with a trailing slash.
bkit
Did you try adding the code only in the httpd.conf file instead of the .htaccess file? You can save it in a separate httpd-extra.conf file and then use the include command at the end of your httpd.conf file.I used this code to achieve exactly what you want, so that i could provide more user friendly urls. The last line should provide that: rewrites (internally) /path as /myphp.php?url=path, so your browser only displays the first.Hope that helps.
misterte
A: 

Sigh.

This line:

RewriteCond %{REQUEST_FILENAME} -D

Should have been:

RewriteCond %{REQUEST_FILENAME} -d
bkit