Forum Moderators: phranque
I can't get my .htaccess file to work. I usually do my rewrites in the httpd.conf file but I'm doing this website on a shared hosting server and they use .htaccess.
This is my .htaccess file:
Options +FollowSymLinks
RewriteEngine On
# Change ---.htm to ---.php
RewriteRule ^([0-9A-Za-z]+).htm $1.php
I have also tried using this which didnt work:
RewriteRule ^([0-9A-Za-z]+)\.htm $1.php
Can anyone help me out?
I will also be adding in a line to convert:
mysite.com/directory/([0-9A-Za-z]+).htm to page.php?page=$1
How would I add that to .htaccess?
My last question is, how can I check to make sure my server is compatible with .htaccess files? Can I find it in phpinfo()?
Thanks in advance for your help!
Wes
To see if you can use mod_rewrite in .htaccess, make a simple rule that rewrites a page name that you know does not exist to one that does exist, such as you home page:
RewriteRule ^non-existent-page\.html$ /page_that_exists.html [L]
If you have no other working rules, you will need some 'set-up'. The first line (Options) is not needed on most servers, and should be removed if not needed; It can cause errors, either if needed and not present, or if present and not needed or not allowed by the host. Showing the full example:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^non-existant-page\.html$ /page_that_exists.html [L]
I tried doing the test and it did not work. I assume .htaccess is not allowed on the server. My friend runs the server so it just means I have to give him a call. None of his clients have dynamic websites so that is why it probably has not been an issue before.
You mentioned the first line causes errors on some servers. Do you mean this line: Options +FollowSymLinks?
If so, should I remove it? What kind of servers does it cause errors on?
RewriteRule ^([0-9A-Za-z]+).htm $1.php
1) a synonym for "[0-9A-Za-z]" that's more reliable for all locales (some expand a-z to aAbBcC... I think) is just "\w", which will also get underbars. However, unless your URLs never have slashes in them (unlikely) that regex will never match, since you're anchoring it to the front of the line. If you remove the anchor, it would match the last URL element, as long as it didn't contain an underbar.
2)But you're not going to have anything non-alphanumeric in a URL, and that regex might cause problems if you ever use a query string or encode a space, so maybe just use either "." or, if you just want the last element of the URL "[^\/]", which will get you anything but a slash.
3) I think it's good practice, if not required, to clearly state what sort of response you're giving, eg. through the [R] field.
What I use on my site, which gets both .htm and .html, is
RewriteEngine On
RewriteRule (.*)\.html?$ $1.php [R]
RewriteEngine On
RewriteRule ([^\/])*\.html? $1.php [R]
Options MultiviewsRewriteEngine On
RewriteRule (.*)\..+$ $1 [R]