denski
denski

Reputation: 2040

Apache mod-rewrite - hidding PHP

I'm try to get my url rewrites to direct whenever .php is added to the end of the url, but essentially I get an error when I include the commented out part.

I can get the .php error to work on it's own, but not with all the other rules in place. in it's current form, it makes every page error.

All other parts are working.

The site is setup so any errors go to one page - error.php

    Options -Indexes
    RewriteEngine On

    ErrorDocument 404 /error.php
    ErrorDocument 301 /error.php
    ErrorDocument 400 /error.php
    ErrorDocument 401 /error.php
    ErrorDocument 403 /error.php
    ErrorDocument 404 /error.php
    ErrorDocument 500 /error.php


    ###### Send any requests for to error ######
    RewriteRule    ^index(\.[A-Za-z0-9-]+)/?$    error.php    [R=404,NC,L]

    ###### changing Validate URL ########
    RewriteRule    ^validate/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/([A-Za-z0-9-]+)/*?$    validate.php?$1=$2&$3=$4    [NC,L]

    ###### Show 404 error page when any request ends with '.php'
            RewriteRule ^(.*)\.php(.?.*)$ error.php [R=404,NC,L]

    #       I also want to allow for anything that contains a get request here. but wanted to get the .php bit working first.



    ###### To remove the trailing slash ######
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^([^/]+)/$ $1.php
    RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
    RewriteRule (.*)$ /$1/ [R=301,L]

Upvotes: 2

Views: 45

Answers (2)

anubhava
anubhava

Reputation: 785581

Change your .php to 404 rule with this rule:

###### Show 404 error page when any request ends with '.php'

RewriteCond %{THE_REQUEST} \.php[?\s/] [NC]
RewriteRule !^/?error\.php$ /error.php [R=404,L,NC]

Upvotes: 1

Abhishek Gurjar
Abhishek Gurjar

Reputation: 7476

Try with below rule,

RewriteRule ^(.+)\.php$ error.php [R=404,NC,L]

Upvotes: 1

Related Questions