homepage Welcome to WebmasterWorld Guest from 54.205.106.111
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Dynamic Query String to Static URL
Dynamic Query String to Static URL
puneetk




msg:4537344
 5:37 am on Jan 19, 2013 (gmt 0)

Following is my current working .htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://www.example.com/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ [%1...] [R=301,NC,L]
RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.php !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1 [R=301,L]
RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]

Problem: Dynamic query strings to static URL not working. Example:

Current URL: http://example.com/venue?popular=wedding-venues-in-chhatarpur

Note: query string is dynamic i.e. popular=value can be anything

I want this to look like: http://example.com/venue/wedding-venues-in-chhatarpur

Pl. advise the RewriteCond and RewriteRule to be applied in .htaccess file which can work. I tried but failed to get the desired result.

 

lucy24




msg:4537364
 7:36 am on Jan 19, 2013 (gmt 0)

Problem: Dynamic query strings to static URL not working.

That's ::cough-cough:: not the only problem. But I don't see anything in the quoted snippet of htaccess that even tries to change the query-string version of the URL to the non-query-string version. So you can hardly blame it for not working. You haven't told it to work :)

Oh, and I'm going to paraphrase an answer I recently gave in another thread, only this time it will be more obvious.

RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://www.example.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L]

RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.php !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME}.php -f
RewriteRule [^/]$ %{REQUEST_URI}.php [QSA,L]


I changed one tiny thing. Do you see why?

puneetk




msg:4537372
 8:12 am on Jan 19, 2013 (gmt 0)

Thank Lucy for revert...

I didn't mention the condition and rule which I had tried... I tried so many version after googling but not even one of them worked. Following is one of them:

RewriteCond %{QUERY_STRING} ^popular=$1
RewriteRule ^venue \.php$ /venue/%1? [R=301,L]

I am sorry but don't see any change except putting them nicely with a line space between every condition. I am looking forward to your help as I am very new to this concept and badly need this issue to resolve. Thank in advance Lucy.

lucy24




msg:4537395
 10:13 am on Jan 19, 2013 (gmt 0)

You've got to stop double-clicking :)

I am sorry but don't see any change except putting them nicely with a line space between every condition.

Yup. That's all there is to see. When your htaccess grows fatter and the rules get more complicated, those spaces will make a big difference. Not to the server, but to you.

RewriteCond %{QUERY_STRING} ^popular=$1
RewriteRule ^venue \.php$ /venue/%1? [R=301,L]

This seems circular.

Nothing has been captured, so what do $1 and %1 refer to?

If you search this Forum you will find a piece of boilerplate on the redirect-to-rewrite two-step. I think that is what you're trying to do here.

g1smd




msg:4537402
 11:39 am on Jan 19, 2013 (gmt 0)

For clarity, the target of each rule that redirects should contain protocol and hostname too.

RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://www.example.com/ [R=301,L]

simplifies to
RewriteRule index\.php http://www.example.com/ [R=301,L]
This redirects to www (but following rules redirect to non-www!) and does not check THE_REQUEST, so this rule could generate an infinite loop. Also missing escaping of literal period.

Rule 2: The [NC] flags are redundant. This rule redirects to non-www whereas Rule 1 redirects to www. Both rules should do the same. The non-www/www rule should also be the LAST of the redirects.

Rule 3: The "add a slash" rule should include the protocol and hostname in the rule target and should be listed before the catch-all non-www/www canonicalisation rule.

Rule 4: This rule should include the protocol and hostname in the rule target and should be listed before the catch-all non-www/www canonicalisation rule. Rather than test REDIRECT_STATUS you should test THE_REQUEST for direct external requests for .php URLs. Many previous posts show the code you need.

Rule 5: The [^/]$ scares me, but the rule should work.

You'll need to add another rule for your "parameters URL request" to "friendly URL" redirect. That rule will need to be before the non-www/www canonicalisation rule. The rule target should include protocol and hostname. It will also need to test THE_REQUEST to be sure the parameters were in the external URL request and not as a result of as previous internal rewrite. The code sample you supplied seems incomplete as $1 and %1 are always undefined.

puneetk




msg:4537417
 1:08 pm on Jan 19, 2013 (gmt 0)

Thanks for your suggestion... but as a novice all going above my head.. I tried using example given in thread for The redirect-to-rewrite two-step but no result..nothing happens. I still get query string in url.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved