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.

Hey guys, how do I rewrite web requests of type http://mydomain.com/mypage.php?id=somenumber where somenumber is an integer TO http://mydomain.com/directory

like http://mydomain.com/mypage.php?id=15 to http://mydomain.com/directory

share|improve this question
    
it is actually the opposite buddy, you rewrite the rules to take directories and transform them into .php?id=15 an example would be, user request mydomain.com/fish and the module will rewrite it to mydomain.com/mypage.php?category=fish, to have the actual id you would do something like mydomain.com/15/fish that would go for mydomain.com/mypage.php?id=15 –  Prix Aug 18 '10 at 1:04
    
Can you help me with that then? rewrite the "directory" to "mypage.php?id=15" –  Daniel t. Aug 18 '10 at 1:52
add comment

1 Answer

up vote 2 down vote accepted

Yeah, well, assuming the comment on your question is what you in fact want, you would do something like:

Somewhere on apache's config:

LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so

On your .htaccess:

RewriteEngine On
RewriteRule ^/directory/$ /mypage.php?id=something [NE,PT]

This would make it so that when you open http://mydomain.com/directory/ it would serve exactly the same content as http://mydomain.com/mypage.php?id=something.

Edit: fixed typo on RewriteEngine and fixed the regexp too... was distracted.

Hope this helps you.

share|improve this answer
    
this one generated internal server error. –  Daniel t. Aug 18 '10 at 1:51
    
Do you have mod_rewrite on your apache config? Something like LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so. Oh I see the problem now... editing answer :P RewriteEngine not Rewire hehe! –  Khai Aug 18 '10 at 2:12
    
I could go for a "rewire" engine for apache! –  warren Aug 18 '10 at 2:55
    
when tested, removing the / preceding directory seems to work RewriteRule ^directory/$ /mypage.php?id=15 [NE,PT] –  Daniel t. Aug 18 '10 at 5:35
    
That makes sense. Since you're putting the RewriteRule in a .htaccess file the RerwiteBase (baseline for all rewritten URLs) is automatically set to the directory where the .htaccess is. Generally I put my RewriteRules on the vhost definition and I keep the preceding / for readability instead of setting RewriteBase. –  Khai Aug 18 '10 at 10:52
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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