views:

318

answers:

4

Hi,

I am trying to rewrite the URL using the mod_rewrite apache module. I am trying to use it as below :

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm wants.php?wantid=$1 [L]

I have created a .htaccess file in the directory from where the files are accessed.

The mod_rewrite is also enabled. With all these done, i still haven't been able to get it working.

Can someone please help me with this? Please let me know if i am missing something

Thanks in Advance, Gnanesh

A: 

I think a leading slash (or rather the lack thereof) might be yoru problem:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/testing.htm /wants.php?wantid=$1 [L]

Otherwise Apache might be looking for the PHP file in /wants/wants.php as it'll be treating it as a relative URL.

cletus
^/wants/testing.htm /wants.php?wantid=$1 [L]
SpliFF
@SpliFF no, that's for .conf files not .htaccess
Greg
+2  A: 

As per OP's comment:

The URL shows mydomain.com/wants.php?wantid=123. I need to make it look like mydomain.com/wants/123

This should work for your case:

RewriteEngine on
RewriteBase /
RewriteRule ^wants/([0-9]+)$ /wants.php?wantid=$1 [L]

It will allow you to use http://yoursite.com/wants/123 and will silently rewrite it to wants.php?wantid=123

duckyflip
A: 

Hmm, so I gave it some thought and I guess you could do something like this ( only changed the regexp according to your comment, if I understood correctly ):

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^wants/\d+$ /wants.php?wantid=$1 [L]

But I guess you could also leave out the $1 reference, and still be able to access the id in your wants.php. So

RewriteRule ^wants/\d+$ /wants.php [L]

should work too, and you can then use something like

<?php 
    $request = split('/', $_SERVER["REQUEST_URI"])[1];
?>

in your wants.php where the last element of the array would be your id ( or anything else you ever decide to rewrite and send to the script ).

Paul
A: 

If you want to use the rule in the .htaccess file in your wants directory, you have to strip the contextual wants/ from the start of the pattern. So just:

RewriteEngine on
RewriteRule ^([0-9]+)$ /wants.php?wantid=$1 [L]

Otherwise, if you want to use the rule in the .htaccess file in your root directory:

RewriteEngine on
RewriteRule ^wants/([0-9]+)$ wants.php?wantid=$1 [L]
Gumbo