mod_rewrite /p/about => ?p=about

Go To StackoverFlow.com

-3

i have a specific question concerning mod_rewrite:

when the user calls:

www.test.com/p/about

the webserver should call internally

www.test.com/?p=about

to access subpages of the website.

but in the adress bar it should still say

www.test.com/p/about

is this anyhow possible with mod_rewrite?

My solutions that did not work out:

  1. Try

    RewriteRule ^/?([-a-zA-Z0-9_+]+)$ index.php?p=$1 [L]

Wrong effect, because it only works with: test.com/about, but I need test.com/p/about!

  1. Try

    RewriteRule ^p/([a-zA-Z]+)$ index.php?p=$1

Error: it redirects correct but the paths on the html file are wrong: eg. /img/pic.png is now /p/img/pic.png

big thanks, stee

2012-04-03 22:52
by Stee
Have you checked any of the almost identical question in the right-hand bar - jeroen 2012-04-03 22:54
Yes it is possible, in fact it's one of the most basic things you can do with mod_rewrite - did you have a go at it yourself yet? What have you tried so far - DaveRandom 2012-04-03 22:54
Or, as the others have said - check out the duplicates on the right : - Pete 2012-04-03 22:58
yes, i've tried many possibilities, i can tell you, its not that easy as it sounds. edited my solution to the initial post that didnt work out - Stee 2012-04-03 23:40
any ideas left? i'm really trying to solve this problem, any help is very appreciated - Stee 2012-04-14 22:14


2

Yes, this is possible with mod_rewrite:

RewriteRule ^p/([a-zA-Z]+)$ index.php?p=$1

Or, more generally:

RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)$ index.php?$1=$2

See: http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule

2012-04-03 22:55
by Pete
sorry, this indeed redirects me to test.php?p=about but the adress in the adressbar also changes to test.php?p=about and this is what i want to avoid! so its not so simple as it sounds in first place - Stee 2012-04-03 23:39
This rule works fine for me - what does the PHP script you're redirecting to do - Pete 2012-04-03 23:42
its a index php-file which displays the corresponding subpage, in this case the about page - Stee 2012-04-04 08:20
Ads