using Apache mod_rewrite to take a variable out of my visible URLs


I'm a bit of a basket case when it comes Regex and Apache mod_rewrite. I thought this one might be simple, but no go. A client asked me to clean up some URLs on a site I did for them. I was suppose to take this:

http://www.accumulist.com/index.php?whatPage=showOneTag&whatTag=webdevelopment

and render it into this:

http://www.accumulist.com/whatPage/showOneTag/whatTag/webdevelopment


I thought this would do it, but no go:

RewriteEngine On
RewriteRule ^whatPage/(.*)$ /index.php?whatPage=$1
RewriteCond %{THE_REQUEST} (.*)index\.php?whatPage=(.*)
RewriteRule ^(.*)index\.php?whatPage=(.*)$ /whatPage/%2 [R=301,L]    


What am I getting wrong here?

lkrubnerAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

caterham_wwwCommented:
- You can't access the queryString from within a RewriteRule
- The RegEx from the condition would match too much

RewriteEngine On
RewriteRule ^whatPage/(.*)$ /index.php?whatPage=$1 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,4}\ /index\.php\?whatPage=([^&\ ]+)\ HTTP
RewriteRule ^index\.php$ /whatPage/%1? [R=301,L]

And finally: Where are you using this? in httpd.conf? or in per-dir context (<directory...> section or .htaccess config. files)?
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
lkrubnerAuthor Commented:
Oh sorry, I should have made clear that this in an .htaccess file at the top level of a domain. We own the server but I didn't feel like hacking into httpd.conf.
0
lkrubnerAuthor Commented:
As before, your answer works like magic. I wish I understood this stuff as well as you do!
0
Cloud Class® Course: Amazon Web Services - Basic

Are you thinking about creating an Amazon Web Services account for your business? Not sure where to start? In this course you’ll get an overview of the history of AWS and take a tour of their user interface.

lkrubnerAuthor Commented:
Could you please tell me what the beginning of this means:

^[A-Z]{3,4}\ /index\.php\?whatPage=([^&\ ]+)\ HTTP
0
caterham_wwwCommented:
Sure.
Start of the string (^)
followed by 3 or 4 ({3,4})
chars in the range A-Z
followed by a space (\ )
followed by /index.php?whatPage= literally
followed by 1 or n (+)
chars not containing a '&' or a space ([^&\ ])
and capured in a backreference
followed by a space (\ )
followed by HTTP literally.
0
caterham_wwwCommented:
Ah, I saw your question in the NG and you transformed your URL further. I have looked only on your snippet, not on the URLs.

That makes it some kind of complex... So you have either one or two parameters and whatPage is always present, while the 2nd param. differs?

RewriteEngine On
# matches on /whatPage/aa --> index.php?whatPage=aa and /whatPage/aa/oneOtherParam/othervalue --> index.php?whatPage=aa&oneOtherParam=othervalue
RewriteRule ^whatPage/([^/]+)(/([^/]+)/([^/]+))?$ /index.php?whatPage=$1&$3=$4 [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,4}\ /index\.php\?whatPage=([^&\ ]+)\ HTTP
RewriteRule ^index\.php$ /whatPage/%1? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,4}\ /index\.php\?whatPage=([^&]+)&([^=]+)=([^&\ ]+)\ HTTP
RewriteRule ^index\.php$ /whatPage/%1/%2/%3? [R=301,L]

> and render it into this:

The source code remains unchanged, of course.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Apache Web Server

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.