0

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

2
  • 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 Commented Aug 18, 2010 at 1:04
  • Can you help me with that then? rewrite the "directory" to "mypage.php?id=15" Commented Aug 18, 2010 at 1:52

1 Answer 1

2

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.

5
  • this one generated internal server error. Commented Aug 18, 2010 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! Commented Aug 18, 2010 at 2:12
  • I could go for a "rewire" engine for apache! Commented Aug 18, 2010 at 2:55
  • when tested, removing the / preceding directory seems to work RewriteRule ^directory/$ /mypage.php?id=15 [NE,PT] Commented Aug 18, 2010 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. Commented Aug 18, 2010 at 10:52

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.