[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

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

Posted on 2006-04-14
6
Medium Priority
?
169 Views
Last Modified: 2010-03-04

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?

0
Comment
Question by:lkrubner
  • 3
  • 3
6 Comments
 
LVL 27

Accepted Solution

by:
caterham_www earned 2000 total points
ID: 16460124
- 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
 

Author Comment

by:lkrubner
ID: 16461282
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
 

Author Comment

by:lkrubner
ID: 16461286
As before, your answer works like magic. I wish I understood this stuff as well as you do!
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:lkrubner
ID: 16461334
Could you please tell me what the beginning of this means:

^[A-Z]{3,4}\ /index\.php\?whatPage=([^&\ ]+)\ HTTP
0
 
LVL 27

Expert Comment

by:caterham_www
ID: 16461509
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
 
LVL 27

Expert Comment

by:caterham_www
ID: 16461764
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

Featured Post

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

As Wikipedia explains 'robots.txt' as -- the robot exclusion standard, also known as the Robots Exclusion Protocol or robots.txt protocol, is a convention to prevent cooperating web spiders and other web robots from accessing all or part of a websit…
If you are running a LAMP infrastructure, this little code snippet is very helpful if you are serving lots of HTML, JavaScript and CSS-related information. The mod_deflate module, which is part of the Apache 2.2 application, provides the DEFLATE…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Suggested Courses
Course of the Month19 days, 11 hours left to enroll

873 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question