2

I wish to accomplish 2 things with this .htaccess file.

1) All requests to .html point to .php eg. user goes to: http://www.mywebsite.com/contact.html Browser loads: http://www.mywebsite.com/contact.php

2) Request to http://www.mywebsite.com/contact will load the contact.php (This should apply to all pages not just the contact page.

Here is my .htaccess file.

Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]

To be honest I have no idea what these are doing. I blended them together from a mismash of articles I read. Any help would be appreciated.

5
  • this RewriteRule ^(.*)$ $1.php [L] is bad; many things can be found there; what if you get domain/a/b?a=t and you rewrite to domain/a/b?a=t.php Commented Mar 19, 2013 at 20:34
  • If it's just for the 2 files/folder, you can use a simple redirect method for each. Commented Mar 19, 2013 at 20:34
  • second, you dont understand the htaccess role; mywebsite.com/contact might not load the contact.php; it will load what ever file you say to load; will try to give you an example Commented Mar 19, 2013 at 20:36
  • Redirect /olddirectory/oldfile.html http://yoursite.com/newdirectory/newfile.html You can also redirect an entire directory by simple using Redirect /olddirectory http://yoursite.com/newdirectory/ Commented Mar 19, 2013 at 20:38
  • I would like this to be site wide, so all html redirect to php. I would also like the user to be able to access the contacts page for example without having to enter the extension. Commented Mar 19, 2013 at 20:41

3 Answers 3

4

Try something like this. Follow the comment thread for security implications, but this is what you're asking to do

RewriteEngine On
RewriteBase /

# Redirect HTML to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*).html$ $1.php [L]

# Otherwise, try PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.php$
RewriteRule ^(.*)$ $1.php [L]

# Lastly, fallback to error page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . 404.php [L]
Sign up to request clarification or add additional context in comments.

2 Comments

THe HTML to PHP gives me this : URL.com/file.php.php.php.php.php...and so forth....
it does yes. I am migrating all of the html files to php, so old links from external sites might still have .html and ofcourse those wont exist, so the browser needs to load .php
0

I need to explain something to you about htaccess file roles;

In this example, htaccess will try and match patterns, as you need in your example;

If a pattern is matched, then some action will happen;

you mark with () all the data than you need to be extracted from the url

For each rule, the first () will have the $1 id, the seccond () will have $2 and so on

if $1 is 'help' it will load the 'helpme.php' file maybe, and not the 'help.php' file; it will load the file that you want it to be loaded;

using $1, $2 ... you can pass parameters and values to the real/translated url request

in my example, i wanted to always use the index.php file, you will use whatever file you need

Options +SymLinksIfOwnerMatch

RewriteEngine on

RewriteRule ^([a-zA-Z-]+)$ index.php?action=$1 [NC,L]

RewriteRule ^(member)-([0-9-]+)$ index.php?action=member&id=$2 [NC,L]

RewriteRule ^([a-zA-Z-]+)/([a-zA-Z-]+)-([0-9-]+)$ index.php?action=$1&saction=$2&sid=$3 [NC,L]

RewriteRule ^([a-zA-Z-]+)/([0-9-]+)$ index.php?action=$1&id=$2 [NC,L]

RewriteRule ^([0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/(.*).html$ index.php?action=details&id=$1&p1=$2&p2=$3&p3=$4 [NC,L]

Comments

0

If you are wanting all of your .html files to point to .php file a simple way of doing so would be

RewriteRule ([\w\d\-_]+)(\.html)? $1.php [L]

This allows you also to use sub-directories

contact.html would redirect to contact.php

/subdir/page.html would redirect to /subdir/page.php and so on

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.