.htaccess redirect .php and .html requests
Asked Answered
P

5

8

I work with a framework called SilverStripe ... we are currently in the process of migrating an old site onto this framework. The problem is that the old site URLs ended with .php or .html whilst in the new site they don't.

I need to amend the second rewrite rule in such a way that I pump the request to main.php without any .html or .php extensions.

In my current .htaccess I have the following rules:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
    DirectoryIndex disabled
</IfModule>

SetEnv HTTP_MOD_REWRITE On
RewriteEngine On

# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]

# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* framework/main.php?url=%1 [QSA]

# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]

Possible solution (still testing):

 RewriteCond %{REQUEST_URI} ^(.*)\.html [OR]
 RewriteCond %{REQUEST_URI} ^(.*)\.php [OR]
 RewriteCond %{REQUEST_URI} ^(.*)$
 RewriteCond %{REQUEST_FILENAME} !-f
 RewriteRule .* framework/main.php?url=%1 [QSA]
Padegs answered 29/12, 2015 at 10:20 Comment(0)
P
4

Add the following rules to your .htaccess file below the # Deny access to potentially sensitive files and folders block of rules:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.+)\.html$
RewriteRule (.*)\.html$ /$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.+)\.php$
RewriteRule (.*)\.php$ /$1 [R=301,L]

The first two lines check that the url is not a directory and is not file.

The third line checks that the url contains either .html or .php.

The forth line removes .html / .php from the url

Phocine answered 5/1, 2016 at 21:55 Comment(7)
@3dogo unfortunately the rule did not triggerPadegs
That is strange. It works for me with my testing. For example if I visit www.example.com/about.html it will redirect to www.example.com/about. Another example www.example.com/about/team.php will redirect to www.example.com/about/team.Phocine
what version of apache are u testing with?Padegs
This is on apache 2.2. What version of apache are you using?Phocine
Server version: Apache/2.2.15 (Unix)Padegs
Same as me. I'm not sure why this works for me but not for you. Where in the .htaccess file have these rules been placed in your project?Phocine
For me this seems the cleanest of them all. @ElGabbu I hope that you noticed that browsers cache the 301 so if you tested some other 301 you should clean your cache before trying new ones.Vu
W
4

You can just tweak your existing rule a bit:

# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
    DirectoryIndex disabled
</IfModule>

SetEnv HTTP_MOD_REWRITE On
RewriteEngine On

# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]

# Process through SilverStripe if no file with the requested name exists.
# Pass through the original path as a query parameter, and retain the existing parameters.
# Strip out .html or .php from request URI
RewriteCond %{REQUEST_URI} ^(.+?)(?:\.(?:html|php))?$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ framework/main.php?url=%1 [L,QSA]

# If framework isn't in a subdirectory, rewrite to installer
RewriteCond %{REQUEST_URI} ^(.*)/framework/main\.php$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . %1/install.php? [R,L]
Worriment answered 9/1, 2016 at 8:57 Comment(0)
D
3

Here is a short solution to the problem

Try the following rule :

RewriteRule ^([^.]+)\.(html|php)$ /framework/main.php?url=$1 [NC,L,QSA]

This will rewrite

/file.php OR /file.html

to

   /framework/main.php?url=$1

pattern explained :

^([^.]+).(html|php)$ matches any request starting with any characters excluding dot followed by a littrel dot char followed by literal php or html in the end of the uri.

Note : this should be the first rule in your htaccess before any other rules.

Denitadenitrate answered 7/1, 2016 at 17:7 Comment(0)
V
3

Try this code:-

RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.html -f 
RewriteRule ^(.*)$ $1.html

Hope this code will work for you :)

Vip answered 11/1, 2016 at 9:12 Comment(0)
J
0

How about the basic...

Redirect 301 /about-us.html http://www.domain.com/about-us

EDIT Now you've mentioned that you have hundreds of these... the above answer is still valid as you can add hundreds of these to the htaccess (I've seen it many times)... however it is very possible also like this...

RedirectMatch 301 (.*)\.html$ http://www.domain.com$1/

The downside of not doing this line by line is now that you may have a specific html file that you do want to allow access to still and that will need adding as an exception to this rule.

Jaunty answered 29/12, 2015 at 11:0 Comment(3)
Because I have about 900 of such URLs :)Padegs
It does answer their question, it just happens that they have now provided new information!Jaunty
is it somehow possible to alter the rule ... RewriteRule .* framework/main.php?url=%1 [QSA] such that what is passed into that url parameter never has a .html or .php extension?Padegs

© 2022 - 2024 — McMap. All rights reserved.