Problems with mod rewrite code


I am new at mod_rewrite and I am trying to rewrite some urls on my site.,

I have a plesk server, and at present my mod_rewrite is not working.

My question is do I have to turn on mod_rewrite in plesk servers?

Do I have to modify the php.ini?

At present I am using this in my .htacess file

RewriteEngine On

AddType x-httpd-php .php .htm .html

AddHandler application/x-httpd-php .php .htm .html

php_flag register_globals on

RewriteRule ^findahome/(homes-[0-9]+).html$ /findahome/listing_details.php?listing_id=$1 [L]

Is anything wrong with the snippet above?


I changed

RewriteRule ^findahome/(homes-[0-9]+).html$ /findahome/listing_details.php?listing_id=$1 [L]


RewriteRule ^/findahome/(homes-[0-9]+).html$ /findahome/listing_details.php?listing_id=$1 [L]

I have also tried

RewriteRule ^/?findahome/(homes-[0-9]+).html$ /findahome/listing_details.php?listing_id=$1 [L]

None of these worked for me. I am using $_GET[‘rewrite’] to let me know if the rewrite is successful or unsuccessful.

These changes did not work for me.


The leading / varies between Apache 1.x and Apache 2.x (the regex with ^/? covers both cases but you should know which version you’re using).

$_GET[‘rewrite’] will NOT return a value because:

  1. The key you’re passing is listing_id, not rewrite


  1. By creating a query string, you’re killing any previous query string (unless you use the QSA flag) so $_GET[‘rewrite’] cannot exist.

One way to see errors like this is to use another flag, the R=301, which forces Apache to display the new URI. When you view that, you can see that the new {REQUEST_URI} is listing_details.php?listing_id={homes-digit(s)} and not listing_details.php?rewrite={whatever}. BTW, you really should just capture the digital value for the value of the listing_id with code like (FOR Apache 2.x):

RewriteEngine on
RewriteRule ^findahome/homes-([0-9]+)\\.html$ findahome/listing_details.php?listing_id=$1 [R=301,L]

… but remove the R=301 and comma after you’re satisfied that the redirection works for you!

As for echo() the $_GET[‘rewrite’] value, better to use print_r($_GET) as you’d also see that ‘rewrite’ is not present but listing_id is (with the value of homes-{digit(s)}). Always best to use the tools you have correctly! :smiley:




First, WELCOME to SitePoint’s Apache forum!

Plesk? I thought they were only on IIS servers, not Apache servers. Okay, let’s assume I’m wrong about Plesk’s residence and get to your two mod_rewrite statements:

RewriteEngine On
# should be 'on' (without the quotes) but I don't think Apache really cares

RewriteRule ^findahome/(homes-[0-9]+).html$ /findahome/listing_details.php?listing_id=$1 [L]
# fine for Apache 2.x; Apache 1.x requires the start anchor to be followed by a /, i.e., ^/,
# and, if you're not sure which you're using, ^/? will work for both

Otherwise, nothing wrong (unless you’re getting a 500 error which would indicate that you do not have mod_rewrite enabled).