redirect all .html extensions to .php - .htaccess

I want to update all the pages on a website to use include for the footer and header. So I have to change a lot of .html pages to .php.
So i'm looking for a way to redirect all pages that end with .html to the same url but ending in .php.

RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
If you want it to be done as a redirect instead of just a rewrite modify the [L] to [L,R]

You could do a more simple approach and have all your html files be processed as php files by adding the following line to your .htaccess
AddHandler application/x-httpd-php .php .html

mod_rewrite to the rescue!
RewriteEngine On
RewriteRule ^(.+)\.html$ $1.php

In your apache httpd.conf file you can add
AddType application/x-httpd-php .html
to make .html files go through the php parser before they are served to the user. You can also add this directive to your .htaccess file. The second method may not work depending on how your host is setup.

If you want an actual HTTP 301 Moved Permanently Redirect
RewriteEngine on
RedirectMatch 301 ^(.*)\.html$ $1.php
or
RewriteEngine on
RewriteCond %{THE_REQUEST} \ /(.+)\.php
RewriteRule ^ /%1.html [L,R=301]

Note that the AddType command will process your existing html file as php. If what you wanted was to replace an existing html file with a new php file you need to use the rewrite rule.

The rule need needs a / in front of $1 otherwise, at least in my cPanel, the file I'm redirected to has the full path, including the home folder etc
RewriteEngine On
RewriteRule ^(.*).html$ /$1.php [L,R]

Related

How can I redirect old URLs to new URLs using htaccess? [duplicate]

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

How "not to remove 'php' extension from specific page?

I am newbie in coding and need your help. I have hidden my .php extension via .htaccess with this code:
RewriteEngine on
RewriteRule ^(.+)\.php$ /$1 [R,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ /$1.php [NC,END]
But there is one page (submit.php) where .php extension is required to work my code in that page, otherwise it shows a blank page. Is there any way around to whitelist specific page form not to remove .php extension?
At the top of your htaccess file ,add the following rule :
RewriteEngine on
RewriteRule ^submit\.php$ - [L]
This will exclude the /submit.php URI from your rules.

How to 301 redirect when have two .htaccess files

I am trying to enable a 301 redirect on my site cornwallcats.co.uk
I seem to have two .htaccess files in my root directory. One shows following and appears as a .txt file:
AddHandler x-mapp-php6 .html .htm
RewriteEngine On
RewriteCond %{HTTP_HOST} ^http://www.cornwallcats.co.uk [NC]
RewriteRule ^(.*)$ http://cornwallcats.co.uk [L,R=301]
The other only shows (and has no filetype?):
AddHandler x-mapp-php6 .html .htm
Any help as to which .htaccess file I should edit would be really helpful!
Thanks
Transfer the contents of .htaccess.txt to .htaccess and then delete the file named .htaccess.txt.
If you want to add 301 redirect directives to your .htaccess file, you need to add them after
RewriteEngine On

.htaccess and redirect urls

I have used the following to rewrite urls on my website
RewriteRule writers/(.*)/ writer-pages.php?page_name=$1
RewriteRule writers/(.*) writer-pages.php?page_name=$1
which works fine, but I have old pages that have an extension /writers/name-here.php and the rewrite above removes the .php how can I redirect the old .php to the the urls without the extension.
Another problem is I don't want redirect all .php pages
Any idea how I can do this. Any help would be much appreciated
RewriteRule writers/(.*).php writers/$1/
you can test your rewrite rules at rewrite-rule-tester
Your 2 rules can be combined into one.
You need an additional rule.
Here is the code:
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /+writer-pages\.php\?page_name=([^\s&]+) [NC]
RewriteRule ^ writers/%1? [R=302,L]
# your original rule
RewriteRule writers/([^/]*)/?$ writer-pages.php?page_name=$1 [L,QSA]

.htaccess redirect folder to a url

I'm trying to redirect a folder and all its sub files to a URL with a .htaccess file.
But
Redirect 301 /abc/cba/ http://www.aaa.com/
Will make /abc/cba/ddd/index.html redirect to http://www.aaa.com/ddd/index.html
What I want is redirect /abc/cba/ /abc/cba/ddd/index.html to http://www.aaa.com/
Could anyone help? Thanks. If anything not clear, please let me know.
By default, Redirect sort of maps the path node to a new path node, so anything after the first path gets appended to the target URL.
Try:
RedirectMatch 301 ^/abc/cba/ http://www.aaa.com/?
Or if you'd rather use mod_rewrite instead of mod_alias:
RewriteEngine On
RewriteRule ^/?abc/cba/ http://www.aaa.com/? [R=301,L]
here's another example of a mod_rewrite rule that worked for me
I wanted to redirect a sub directory to the root of the same domain.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^sub_directory/(.*)$ /$1 [R=301,NC,L]
</IfModule>
more examples can be found here:http://coolestguidesontheplanet.com/redirecting-a-web-folder-directory-to-another-in-htaccess/
I perfer the following method:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/somedir [NC]
RewriteRule /(.*) http://somesite.com/lost/$1 [R=301,L]
I had to reroute urls from old site version to new version, so here is what I did to reroute any links from about-us/* to about-us.html
RewriteEngine on
RewriteRule ^about-us/(.*)$ about-us.html [R=301,L]
What it doesn't do is rewrite something like domain.com/about-us/thing.html => domain.com/about-us.html .
It does work for things without extensions domain.com/about-us/something-in-url => domain.com/about-us.html
I added the lines below to redirect .jpg and .png, but it didn't work for .html, I can't find out why.
RewriteRule ^about-us/(.*).jpg about-us.html [R=301,L]
RewriteRule ^about-us/(.*).png about-us.html [R=301,L]

Resources