Mod Rewrite dilemma

Any questions involving matching text strings to patterns - the pattern is called a "regular expression."

Moderator: General Moderators

Post Reply
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Mod Rewrite dilemma

Post by AlanG »

Got a mod rewrite problem, hopefully someone can help. :)

I have a dynamic url I want changed.

Original: index.php?page=home
Target: home.php

I tried the following code but it returns a 500 error. Any ideas? Thanks for any help. :)

Code: Select all

RewriteEngine On
RewriteRule ^([^/]*)\.php$ /index.php?page=$1 [L]
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Mod Rewrite dilemma

Post by Benjamin »

Code: Select all

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\.php$ index.php?page=$1 [L,QSA]
I believe that should work for you. Otherwise check the server error log. The 500 error could be another issue, but more than likely it was due to permissions.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite dilemma

Post by AlanG »

Works perfectly. :) Thank you very much. I'll have a look into the two other arguments you gave. :) Thanks again.
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Mod Rewrite dilemma

Post by Benjamin »

Welcome. The other two conditions specify to only rewrite the request if a file or directory by the same name does not exist. Also note that I removed the / from in front of index.php in the RewriteRule.
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Mod Rewrite dilemma

Post by AlanG »

Thanks for the help earlier. I just need another adjustment so it's working perfectly. I want all my pages to show up as .html.
index.php?page=home now shows up as home.html but i also want contact.php to show up as contact.html, just to keep the site consistant. Any thoughts?

Code: Select all

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
 
RewriteRule ^(.*)\.html$ /~user/index.php?page=$1 [QSA,NC]
RewriteRule ^(.*)\.html$ /~user/$1.php [NC]
 
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
 
ErrorDocument 404 /~user/index.php?page=page+not+found
Thanks alot for the help. :)
Post Reply