Forum Moderators: phranque

Message Too Old, No Replies

Yet another "Help me with my .htaccess" post :)

What am I doing wrong?

         

wfernley

8:50 pm on Apr 27, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey everyone,

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

jdMorgan

5:34 pm on Apr 28, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> My last question is, how can I check to make sure my server is compatible with .htaccess files?

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 request that non-existent page from your server, it should give you the page that exists.

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]

Jim

wfernley

4:02 pm on Apr 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks for your reply Jim.

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?

heisters

8:15 pm on Apr 30, 2006 (gmt 0)

10+ Year Member



RewriteRule ^([0-9A-Za-z]+).htm $1.php

That's a bit messy and unnecesarily long. I'm not sure if that's what's causing your problem, but a slightly simpler regex is always nice:

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]

or, if I just wanted the last URL element, so that it doesn't interfere with some other Rule, or whatever reason:

RewriteEngine On
RewriteRule ([^\/])*\.html? $1.php [R]

BUT I don't actually use that, because file extensions are *sooo* 20th century. Better yet, enable content negotiation and do something like

Options Multiviews

RewriteEngine On
RewriteRule (.*)\..+$ $1 [R]


Enjoy.

jdMorgan

10:26 pm on Apr 30, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> You mentioned the first line causes errors on some servers. Do you mean this line: Options +FollowSymLinks?
Yes.

> If so, should I remove it?
Only if it causes a 500-Server Error.

> What kind of servers does it cause errors on?
Servers with AllowOverride None set in httpd.conf

Jim

wfernley

11:50 am on May 2, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thank you for both your posts!

I will keep the info in mind.