Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Possible Duplicate:
Make post title as a slug in url by mod rewrite

I have a url:

http://www.example.com/details.php?var=100&p=99

I want to rewrite it as using mod rewrite

http://www.example.com/100/99

I am using following rewite_rule:

RewriteRule ^([0-9]+)/([0-9]+) $  http://www.example.com/details.php?var=$1&p=$2   [L]

but it is not working. If I type http://www.example.com/100/99 it redirects to http://www.example.com/details.php?var=100&p=99 url. Please help me in this issue. Thanks

share|improve this question

migrated from stackoverflow.com Jun 22 '11 at 17:22

This question came from our site for professional and enthusiast programmers.

marked as duplicate by Caleb, Chopper3 Jun 24 '11 at 7:41

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

    
i missread the question, deleting my answer –  Ibu Jun 22 '11 at 6:36
    
but you said that its redirecting to the correct place?!! what is the problem with the redirect? –  Ibu Jun 22 '11 at 6:37
    
@lbu Its being reverse redirect. I want like directory structure in url and want to get rid from perimeters –  Sohail Jun 22 '11 at 6:39
add comment

3 Answers

up vote 0 down vote accepted

The prblem in your approach is

  1. ^([1-9]+) the URI starts with / - your telling the rule to check for some digits without /
  2. There could be zeros in your URL so having 1-9 will not allow you to have zero.

Try this:

RewriteRule ^/([0-9]*)/([0-9]*) http://www.example.com/details.php?var=$1&p=$2   [L]

You can validate your rewrite-rule here - http://martinmelin.se/rewrite-rule-tester/

share|improve this answer
add comment

If it's about the _redirect_, then remove the http://... prefix from the target part.

RewriteRule ^(\d+)/(\d+)$  details.php?var=$1&p=$2   [L]

Make this the first rule.

Also keep in mind that you have to adapt your application to actually generate /123/456 urls. The rewriterule only works on incoming requests, it does not alter pages.

share|improve this answer
    
It works but images are not showing on the page. –  Sohail Jun 22 '11 at 6:51
1  
If the page has a new virtual URL, then the relative references stop to work. The easy workaround is to embed <base href="/index"> in the <head> of your pages. –  mario Jun 22 '11 at 6:54
    
your are really super Mario. Thanks buddy –  Sohail Jun 22 '11 at 7:04
add comment

Include 0 in the regular expression:

RewriteRule ^/([0-9]+)/([0-9]+)/?$  http://www.example.com/details.php?var=$1&p=$2   [L]

The reverse would be:

RewriteCond %{QUERY_STRING} ^var=([0-9]+)\&p=([0-9]+)$ 
RewriteRule ^details\.php$ http://www.example.com/%1/%2 [R=301,L]
share|improve this answer
    
problem is still persisting. –  Sohail Jun 22 '11 at 6:35
    
Added the reverse of the rewrite, in case you want that instead. –  aorcsik Jun 22 '11 at 6:40
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.