Forum Topic: Trying to hide .php

Forum: .htaccess Forum : Redirecting • Posted by francadaval • Updated:

Hi,

I aplied this rule and it works fine:

RewriteCond %{REQUEST_URI} !.php$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]

so www.domain.com/page?a=12&b=5 runs in page.php.

Now I’m trying requests to page.php to return a 404 error.

This rule works fine:

RewriteCond %{REQUEST_URI} .php$
RewriteRule .* - [L,R=404]

The problem is that with both rules in my .htaccess any request to www.domain.com/page returns a 404 error code. I thought that [L] made the server to stop rewriting so after rewriting page to page.php it had to stop.

Why is it rewriting again to a 404 response?

How can I make it work?

5 Replies to “Trying to hide .php”

Posted by Jeff Starr

Hi Francisco,

Try doing the second (404) redirect with RedirectMatch instead, something like this:

RedirectMatch 404 \.php

Let me know how it goes!

Posted by francadaval

Both requests (www.domain.com/page and www.domain.com/page.php) returns a 404 error. This is my .htaccess:

# Deshabilitar listado de directorios.
Options All -Indexes

# Idioma por defecto espa?ol
DefaultLanguage es

RedirectMatch 404 \.php

# Enable mod-rewrite
RewriteEngine On

# Redirect page.php to 404 error
# RewriteCond %{REQUEST_URI} .php$
# RewriteRule .* - [L,R=404]
# Redirect page to page.php

RewriteCond %{REQUEST_URI} !.php$
RewriteCond %{REQUEST_URI} !.html$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]
Posted by francadaval

In modrewrite.com I’ve read this:

After a URL request matches a rule, and changes are applied, the request is sent back to the main configuration file and treated like it is a new URL request.

Is there any condition to avoid it?

Posted by francadaval

Well, I’ve found the solution and it’s pretty simple.

Adding a redirection status check I avoid the redirection to 404 when the request to page.php already come from a redirection.

Those are my rules now:

# Redirect page.php to 404 error if it doesn't
# come from redirection.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} .php$
RewriteRule .* - [L,R=404]

# Redirect from page to page.php
RewriteCond %{REQUEST_URI} !.php$
RewriteCond %{REQUEST_URI} !.html$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1.php [L]
Posted by Jeff Starr

That’s good news, Francisco! Glad to hear you got it working, and thank you for sharing the solution in your follow-up post. Cheers!