URL rewriting with mod_rewrite in .htaccess

I am trying to rewrite url using apache mod_rewrite module. I am trying to use it like 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 which the files are available.

Also mod_rewrite is included. When all this is done, I still haven't been able to get it to work.

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

Thanks, Advance, Gnanesh

0


a source to share


4 answers


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]

      

This will allow you to use http://yoursite.com/wants/123 and rewrite it to . PHP? wantid = 123

+2


a source


I think the leading slash (or rather the lack of it) might be a yoru issue:

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

      



Otherwise Apache might look for the PHP file in /wants/wants.php as it will treat it as a relative URL.

0


a source


Hmm, so I thought about this, and I think you could do something like this (only changed the regex to match your comment if I understood correctly):

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

      

But I think you could also leave the link at $ 1 and still have access to the id in your wants.php

. So

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

      

should work too and you can use something like

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

      

in yours wants.php

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

0


a source


If you want to use a rule in the .htaccess file in your wish directory, you need to uncheck the contextual wants/

from the beginning of the template. So simple:

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

      

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

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

      

0


a source







All Articles