0

I've set up a .htaccess file to rewrite the url example.com/foo too example.com/foo.php I can separately get the URL to redirect to error.php if it ends in .php, but I can't seem to do both together.

I've seen other sites hide .php and I want to understand how to do that.

Here's what I've tried:

//.htaccess file

RewriteEngine On    # Turn on the rewriting engine
RewriteRule    ^([A-Za-z0-9-_]+)/?$    $1.php    [NC,L]
RewriteRule    ^([A-Za-z0-9-_]+).php?$    error.php    [NC,L]

NOTES

Here is what worked for me. I'm going to leave it hear for reference as I'm not sure it all necessarily is necessary.

    RewriteEngine On

    RewriteRule (.*)index$ http://%{HTTP_HOST}/error [R=404]
    RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php(\?.*)?\ HTTP/
    RewriteRule ^ http://%{HTTP_HOST}/error [R=301]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule .* $0.php
    ErrorDocument 404 /error.php 
    ErrorDocument 301 /error.php 
2
  • 1
    It is unclear what you ask. That means you want to redirect all incoming requests to that error.php script internally? Commented Mar 26, 2017 at 18:27
  • @arkascha, thanks I've edited the question. Essentially, I've seen other sites hide .php and I want to understand how to do that while also rewriting the URLs that don't end in .php but do exist, like example.com/foo Commented Mar 26, 2017 at 18:31

2 Answers 2

1

Considering your comment to your own question it seems this is what you are looking for: If the request targets something to what a php script exist with the same name but an added .php file name extension, then internally rewrite to that. Rewrite to /error.php in all other cases, if the request resource does not directly exist in the file system.

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /error.php [L]

And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).

Sign up to request clarification or add additional context in comments.

8 Comments

thanks, do you mean between the <VirtualHost *:80></VirtualHost> tags, I've been googling references to 'apache2 host configuration' but I haven't found a concrete answer of precisely what I'm looking for.
Sure, those tags enclose the static configuration of (one of) your (virtual) host(s). It is read and interpreted once when the http server process is started. That is opposed to dynamic configuration files spread around your ??DOCUMENT_ROOT`'s folder hierarchy.
Here is what worked for me thought I feel I may have some stuff in there that isn't useful? (I've added it into my question above).
I'm after editing your answer rather than the question!
I have no idea where that code came from you posted or what it is supposed to do in details: fact is that it is chaotic and has some details that leave me surprised that it does not crash at all...
|
1

I do it using this two rules:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.