Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

I have this httpd.conf ...

<IfModule mod_rewrite.c>
     RewriteEngine On
     # Rewrite /foo/bar to /foo/bar.php
     RewriteRule ^([^.?]+)$       %{REQUEST_URI}.php [L]
</IfModule>

The above rule works fine for e.g. ://example.com/dir1/file BUT for ://example.com/dir1/file?q=&category=5&brand=BRAND it just rewrites it as ://example.com/dir1/file/?q=&category=5&brand=BRAND which off course throws a "Page Not Found" Error, because /file/ dir doesn't exit.

THE same rule with [R] correctly redirects to ://example.com/dir1/file.php?q=&category=5&brand=BRAND but I don't want the redirect. I just want the "internal" rewrite.

Can sb please Help? It's LAMP, Centos Apache 2.

(This code was working perfectly on PLESK, vhost.conf) But on httpd.conf it just doesn't....

share|improve this question
    
Please use the code or block-quote tags. –  Dennis Williamson Jul 5 '09 at 21:55
    
sorry _ –  user11665 Jul 5 '09 at 22:04

3 Answers 3

(I haven't tried your second suggestion, but I fixed it) Some FREE Knowledge to Googlers out there: The Apache version matters! When you scan through the documentation, MAKE SURE you see stuff for the Apache version you have.

My problem was that I used PLESK for everything. This block on vhost.conf:

<IfModule mod_rewrite.c>
    		RewriteEngine On

    		# Rewrite /foo/bar/ to /foo/bar/index.php
    		RewriteRule /([^.?]+)/$      %{REQUEST_URI}index.php [L]

    		# FIX: If request_uri is just the domain name, then redirect to index.php.
    		RewriteCond %{REQUEST_URI}   ^/$ [NC]
    		RewriteRule /(.*)           %{REQUEST_URI}/index.php [L]

    		# Rewrite /foo/bar to /foo/bar.php
    		RewriteRule /([^.?]+)$       %{REQUEST_URI}.php [L]

    		# Return 404 if original request is /foo/bar.php
    		RewriteCond %{THE_REQUEST}   "^[^ ]* .*?\.php[? ].*$"
    		RewriteRule .*               /error [L,R]
    	</IfModule>

was working like a charm.

BUT on CentOS/Apache 2 it just didn't. The problem was the slash at the beginning and Apache's auto-trailing-slash-adding (a gift from the newer versions). This auto-trailing-slash-adding was not woprking when a script was name /file and there was and a dir named /file/. So I added these 2 lines:

<IfModule mod_rewrite.c>
    		RewriteEngine On
    		**DirectorySlash Off**

    		# Rewrite /foo/bar/ to /foo/bar/index.php
    		RewriteRule **^/?**([^.?]+)/$      %{REQUEST_URI}index.php [L]

    		# FIX: If request_uri is just the domain name, then redirect to index.php.
    		RewriteCond %{REQUEST_URI}   ^/$ [NC]
    		RewriteRule **^/?**(.*)           %{REQUEST_URI}/index.php [L]

    		# Rewrite /foo/bar to /foo/bar.php
    		RewriteRule **^/?**([^.?]+)$       %{REQUEST_URI}.php [L]

    		# Return 404 if original request is /foo/bar.php
    		RewriteCond %{THE_REQUEST}   "^[^ ]* .*?\.php[? ].*$"
    		RewriteRule .*               /error [L,R]
    	</IfModule>

How cool is that?

share|improve this answer
    
Are the double asterisks intended to be bold? Because they don't work in a code block here and I don't think it's part of the rewrite syntax. Also, the question marks inside square brackets don't serve any purpose, since the query string is not part of the pattern being searched in a RewriteRule. (The other question marks, of course, do have meaning as the 0 or 1 quantifier). BTW, it might have been helpful if you had posted the whole set of rules in your question instead of just part. –  Dennis Williamson Jul 6 '09 at 13:08

This may get you a little closer:

RewriteRule ^([^.?]+)(.*)$       $1.php$2 [L]

but someone else can probably provide something better.

Try this one:

RewriteCond %{QUERY_STRING}     (.*)
RewriteRule ^(.*/?[^.]+)$       $1.php?%1    [L]
share|improve this answer
    
This just threw a "500 Internal Server Error". NOTE: It is CentOS 64 bit, not i386. –  user11665 Jul 5 '09 at 22:33
    
The OS shouldn't make any difference. –  Dennis Williamson Jul 6 '09 at 3:27

If all you're trying to do is hide the filename extension from the URLs why don't you simply enable the "MultiViews" option for the path? I do this routinely on my web sites so I can intermix .html and .php pages but leave the extension out of the URL.

If you have "AllowOverride All" or "AllowOverride Options" configured for you web page you can simply add a .htaccess file with the following:

Options +MultiViews

That will enable MultiViews and Apache will use MIME negotiation to find an available file within the path, or if you have full control of the Apache configuration file you can simply add "MultiViews" to the "Options" line within the configuration file.

If you want to see an example you can check out my GPG Policy URL at http://undergrid.net/legal/gpg which is ran entirely from my /legal/gpg.php page.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.