Another rewrite rule conundrum

I have the following rewrite rule in my .htaccess file on 2 or 3 of my sites

RewriteRule ^([A-Za-z0-9-]+)$ $1.php [PT,L]

which works fine for sites that have urls like
www.domain.com/contact-us

but I’m now trying to get it to work on another site with urls like
www.domain.com/xx/contact-us
where xx is any 2 lowercase letters.

I have played and played with the rewrite rule without luck. Can anyone help please?

gandy,

Is the contact-us.php script in the xx directory? If so, add a / within the character range definition (move the - to the first character where it belongs and then add the / after the 9). If not, simply remove the start anchor (the ^) … but expect relative links to be offset by one directory level (resolve that issue by using absolute INTERNAL links rather than EXTERNAL or RELATIVE links).

Regards,

DK

Thanks DK

Yes, the contact-us.php script is in the xx directory. I thought the - belonged at the end of the range so it didn’t get confused for a range.

I have been using relative links as I’m trying to use the same .htaccess on my test server where the url is
http://localhost/www/domain.com/en/contact-us.php
as well as on the live site where it is
http://www.domain.com/en/contact-us.php

Gandy,

No problem.

Actually, the - is “required” to be the first character in order to avoid being interpreted as a metacharacter within a character range definition. Of course, in the real world, the - can be anywhere where it can not possibly be interpreted as the metacharacter (including the last position) but my mentioning it was specifically so you would NOT add the / AFTER the -.

Just a side comment: localhost/www/domain.com looks pretty strange … but that’s because I’ve been using virtual hosts on my test (localhost) server for so many years. May I recommend that you:

  1. Update your hosts file to add 127.0.0.1 domain (do NOT use Domain.com on your localhost as you want to be able to see your production website, too!) - do this as ADMINISTRATOR or you will not be able to save the file!

  2. Update your httpd-vhosts.conf file to include your domain virtual host - just follow the example in the file or come back to ask for what’s needed). Ditto the Admin comment IF Apache is in Program Files (a good reason NOT to install there!).

  3. Update your httpd.conf file to have it include your extras/httpd-vhosts.conf file - with the same Admin comment as above.

  4. Restart Apache and access your localhost’s new virtual domain as http://domain/{whatever}.

You’ll come to LOVE being able to use virtual domains to access your master copies of your websites with little change in the .htaccess ({HTTP_HOST} will either contain .com or not and you can fork your redirections based on that).

Regards,

DK

Thanks for clarifying the - DK.

I shall have to get my head around your recommendations. The reason I started using the full domain name locally is it makes FTPing easier. I have domains with several various suffixes: .com, .co.uk, .eu, .org.uk; even a .me.uk and a .info !

Ciao G

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.