0

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

Dan
  • 69
  • 1
  • 9

3 Answers3

2

put the following rule at top ( bellow RewriteEngine )

RewriteRule ^filebooking\.php$ - [L]

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

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Thank you very much. I've tried your method but I am still getting a 404 error. Does it might have something to do that the file booking.php runs over https? – Dan Jul 01 '16 at 08:03
1

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
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • Thank you very much for your answer. I've tried your method but I am still getting a 404 error and the file extension does not seem to be removed. Does it might have something to do that the file booking.php runs over https? – Dan Jul 01 '16 at 08:03
  • Maybe I misunderstood your question, but I thought you want to exclude `booking.php` from the rewriting. – Olaf Dietsche Jul 01 '16 at 08:14
  • Encryption shouldn't matter here, because you have no conditions checking this. – Olaf Dietsche Jul 01 '16 at 08:17
  • I erroneously wrote `filebooking.php` instead of `booking.php`, maybe that's it? – Olaf Dietsche Jul 01 '16 at 08:20
0

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.

Community
  • 1
  • 1
KJaeg
  • 698
  • 3
  • 7
  • 23