Hi there, I need a rewrite that’ll make the url equal to:
http://www.example.com/contact
OR:
http://www.example.com/contact/
Where the original URL would be:
http://www.example.com/contact.php
This URL, I don't want. I want only the first two.
The ‘contact’ in the url can be anything. I have this in my .htaccess file:
RewriteEngine on
RewriteRule ^(\\w+)$ $1.php [L]
I really don’t know how to get it to do what I want it to do. :headbang: I’ve tried for hours to get it to work. :headbang:
In theory that code should do what you want. Are you sure mod_rewrite is enabled?
It could be that you’re on Apache 1.x, in which case you should add a \ before (\w+) and replace (\w+) with ([a-zA-Z0-9]+), i.e.
RewriteEngine on
RewriteRule ^/([a-zA-Z0-9]+)$ $1.php [L]
It could also be that your host is using Mass Virtual Hosting, in which case you need a put a / before $1.php
If I were you I’d start with a very simple rule and see if that does anything
Options +FollowSymLinks
RewriteEngine On
RewriteRule test http://www.google.com/ [L,R=302]
then request any URL that contains “test” and see if you’re taken to google. If you’re not you’d need to figure out why mod_rewrite isn’t working (you might want to contact your host for that).
Ugghhh! It says that the page has a redirect loop…? Here’s the full .htaccess file…
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
################################################################
# Make all requests have the www. in them
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\\.org
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent]
# Make the '.php' be removed from all URL's
RewriteRule ^(\\w+)$ $1.php [L]
RewriteCond %{THE_REQUEST} \\.php
RewriteRule ^(.*)\\.php$ $1 [L,R=301]
The last two lines were the ones that you suggested. It just won’t work! :headbang: lol
# Use PHP5 Single php.ini as default
AddHandler application/x-httpd-php5s .php
################################################################
# Make all requests have the www. in them
RewriteEngine on
# Redirect everything that is not www.example.com to www.example.com
RewriteCond %{HTTP_HOST} !^www\\.example\\.org$ [NC]
# Use .? / %{REQUEST_URI} instead of (.*) / $1 -- better and faster
RewriteRule .? http://www.example.org%{REQUEST_URI} [L,R=permanent]
# The rules should be the other way around, and the first should use a FQDN instead of just a path
RewriteCond %{THE_REQUEST} \\.php
RewriteRule ^(.*)\\.php$ http://www.example.org/$1 [L,R=permanent]
RewriteRule ^(\\w+)$ $1.php [L]
You did replace [noparse]www.example.org[/noparse] with your own domain, right?
BTW. While you’re testing it’s best to change “permanent” to 302, because browsers can (and do) cache permanent redirects. Meaning you can change the .htaccess until your blue in the face but it won’t have any effect because the browser is just doing what it was told to do earlier.
Weird. I just tested the code I posted in post #6 and it works fine – no errors, and completely according to spec. Could you post the complete .htaccess you have now please?