Dan
Dan

Reputation: 69

Remove file extension php except for one file

My htaccess file perfectly removes all file extensions from all php files which is fine, however I would need the file/page booking.php to be excluded from that and to be loaded with the .php extension. What is the best way to have this achieved ? I am using Apache 2.4.7 on Ubuntu 14.04

My current htaccess file:

    <IfModule mod_rewrite.c>

     Options +FollowSymLinks -MultiViews -indexes
     RewriteEngine On
     RewriteBase /

     RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
     RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]

     RewriteCond %{THE_REQUEST} \s/+sections/(.+?)(?:\.php)?[/?\s] [NC]
     RewriteRule ^ %1 [R=301,L]

     # remove index
     RewriteCond %{THE_REQUEST} /index(\.php)?[\s?/] [NC]
     RewriteRule ^(.*?)index(/|$) /$1 [L,R=301,NC,NE]

     # remove .php; use THE_REQUEST to prevent infinite loops
     RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
     RewriteRule (.*)\.php$ $1 [L,R=301]

     # remove index
     RewriteRule (.*)/index$ $1/ [R=302]
     # remove slash if not directory
     RewriteCond %{REQUEST_FILENAME} !-d
     RewriteCond %{REQUEST_URI} /$
     RewriteRule (.*)/ $1 [R=301,L]

     # add .php to access file, but don't redirect
     RewriteCond %{REQUEST_FILENAME}.php -f
     RewriteCond %{REQUEST_URI} !/$
     RewriteRule (.*) $1.php [L]

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

    </IfModule>

I've tried a few methods however with no result so far as always resulting in 404 and never preserving the file extension. Some expert help would be greatly appreciated

Upvotes: 0

Views: 1118

Answers (3)

Olaf Dietsche
Olaf Dietsche

Reputation: 74028

Depending on what you need, you can exclude filebooking.php from any rewrite at all by exiting the rule chain at the beginning

RewriteRule ^booking\.php$ - [L]

See RewriteRule

- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.


If it's not every rule, another approach would be to prefix each relevant rule with a RewriteCond and exclude filebooking.php

RewriteCond %{REQUEST_FILENAME} !booking\.php

Upvotes: 1

KJaeg
KJaeg

Reputation: 686

A little googling gave me this possible way to solve this, found in this SO-post:

RewriteCond %{REQUEST_URI} !^(.*/)?index\.php$ [NC]

In the upper example the user wanted to exclude his index.php. The best answer of the upper link shows different approaches as well. Just try to replace it with your specific file name.

Upvotes: 0

Amit Verma
Amit Verma

Reputation: 41219

put the following rule at top ( bellow RewriteEngine )

RewriteRule ^filebooking\.php$ - [L]

filebooking.php will get passed through unchanged via the - target.

Upvotes: 2

Related Questions