mod_rewrite and query string

http://www.domain.com/gallery/mygallery/index.html

should be rewrited into

http://www.domain.com.com/index.php?gallery=mygallery but the url I have to see in my browser is http://www.domain.com/gallery/mygallery/index.html


and the same for :

http://www.domain.com/gallery/mygallery/1/index.html 

should be rewrited into

http://www.domain.com/index.php?gallery=mygallery&img=1
LVL 8
us111Asked:
Who is Participating?
 
rama_krishna580Commented:
Hi,

More Advanced - the Query String:

The query string is passed in separately from the URL. This means that a simple regex doesn't necessarily do the trick, but a compound statement using RewriteCond (condition) is required.

RewriteCond %{QUERY_STRING} id=([^&;]*)
RewriteRule ^/$ http://%{SERVER_NAME}/%1/? [R]
RewriteRule ^/([^/]*)/?$ /index.php?id=$1 [L]

The RewriteCondition matches only when the following condition is true, and continues until a "last" [L] is stated. The Condition's backreferences are different, using the % prefix, and their scope lasts beyond the Condition line.

This above example would translate "/?id=home" into "/home/", and then re-assign the value of "home" to the id HTTP_GET_VAR. One more thing to notice here is that the the second line has a trailing ? - this is used to negate copying of the query string into the new, re-directed URI.

More Reference Links:

http://www.engelschall.com/pw/apache/rewriteguide/
http://httpd.apache.org/docs/mod/mod_rewrite.html
http://httpd.apache.org/docs/misc/rewriteguide.html

R.K
0
 
caterham_wwwCommented:
So you're planning to use the RewriteRules in a .htacccess file? Or in httpd.conf?

That would be for a .htaccess file:

RewriteEngine on
RewriteRule ^gallery/([^/]+)/index\.html$ /index.php?gallery=$1 [L]
RewriteRule ^gallery/([^/]+)/([0-9]+)/index\.html$ /index.php?gallery=$1&img=$2 [L]

for httpd.conf, just add a leading slash in the pattern:

RewriteEngine on
RewriteRule ^/gallery/([^/]+)/index\.html$ /index.php?gallery=$1 [L]
RewriteRule ^/gallery/([^/]+)/([0-9]+)/index\.html$ /index.php?gallery=$1&img=$2 [L]
0
 
us111Author Commented:
>>>it is now classified as abandoned
No, sorry but leave this question opened for the moment


0
Cloud Class® Course: MCSA MCSE Windows Server 2012

This course teaches how to install and configure Windows Server 2012 R2.  It is the first step on your path to becoming a Microsoft Certified Solutions Expert (MCSE).

 
periwinkleCommented:
Is there still a problem that we can assist you with?  What happened when you followed the advice given by the experts above?
0
 
us111Author Commented:
No no I think it's ok. But I need to test...
0
 
us111Author Commented:
Please leave it opened.

I know you have to clean but this question has only 3 month. I think it's better to clean questions from 2005!

I'm gonna do my best to close it.  btw i will increase the points
0
 
us111Author Commented:
I'm agree with your arguments

Anyway, as I have no time to test I suppose it works......

Points splited
0
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.

All Courses

From novice to tech pro — start learning today.