14

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> 
1
  • Based on the behaviour you're describing I think that there's something wrong with another part of your .htaccess file, so would you mind posting the whole thing? Commented Jun 28, 2010 at 18:45

4 Answers 4

12

This line:

RewriteCond %{REQUEST_FILENAME} -D 

Should have been:

RewriteCond %{REQUEST_FILENAME} -d 
Sign up to request clarification or add additional context in comments.

Comments

8

Don't bother with Mod Rewrite, there is a directive for it

<Location /some/path> DirectorySlash On SetHandler some-handler </Location> 

http://httpd.apache.org/docs/2.2/mod/mod_dir.html

1 Comment

DirectorySlash Off does the opposite, it disables the mod_dir automatic slash append when the request resource is a directory. The default is on. Why is not working for the OP is unknown, maybe mod_dir is not loaded
0

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.

2 Comments

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.
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.
0

Try this. It will add trailing slash to directory requests which don't have trailing slash.

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.