mod_rewrite Problem!

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:

Help is appreciated please. :slight_smile:

Thanks,

Paul

Hi Paul,

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).

Hi ScallioXTX,

Thanks for answering! This code does work:

RewriteEngine on
RewriteRule ^(\\w+)$ $1.php

And it takes me to the correct page, but I want to only be allowed access to:

http://www.example.com/contact
OR
http://www.example.com/contact/

I don’t want to be able to put the .php after contact. I just don’t know how to do that…? :stuck_out_tongue:

Thanks for the help. :slight_smile:

Paul

Ah, I get it :slight_smile:

Maybe you could just strip .php from the URL if it is requested?

Something like


RewriteEngine On
RewriteCond %{THE_REQUEST} \\.php
RewriteRule ^(.*)\\.php$ $1 [L,R=301]

I haven’t tested it, but it should redirect /contact.php to /contact :slight_smile:

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: :stuck_out_tongue: lol

Okay, try this:


# 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]

Slight problem with that…It redirects to IANA — Example domains :\ Not sure what’s goin on with it…?

You did replace [noparse]www.example.org[/noparse] with your own domain, right? :wink:

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.

Yea, I did replace it. And I’ll try the 302. Just one sec. :slight_smile:

Ok, so that didn’t work. Could I just add a RedirectMatch to the end? Like


RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\\.org
RewriteRule ^(.*)$ http://www.example.org/$1 [R=permanent]

RewriteRule ^(\\w+)$ $1.php
RedirectMatch permanent ^(\\w+)$ /$1/ [L]

The problem with that is that the Redirect Match doesn’t work. I can’t figure out how to make it work. :stuck_out_tongue: lol

So at this point, what is working now (if anything) and what isn’t?

Absolutley nothing works. It gives me a 500 error. :stuck_out_tongue:

To be honest with you, I have no idea what’s wrong. :stuck_out_tongue:

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?

I just emptied the cache from yesterday and today. :stuck_out_tongue: It works now. :stuck_out_tongue: Thanks a ton. :slight_smile: I really appreciate the help! :slight_smile:

Nice, glad it worked out :slight_smile: