Welcome to WebmasterWorld Guest from 54.196.214.35

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Dynamic Query String to Static URL

Dynamic Query String to Static URL

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

New User

joined:Jan 19, 2013
posts: 4
votes: 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.
7:36 am on Jan 19, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12717
votes: 244


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?
8:12 am on Jan 19, 2013 (gmt 0)

New User

joined:Jan 19, 2013
posts: 4
votes: 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.
10:13 am on Jan 19, 2013 (gmt 0)

Senior Member from US 

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month

joined:Apr 9, 2011
posts:12717
votes: 244


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.
11:39 am on Jan 19, 2013 (gmt 0)

Senior Member

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month

joined:July 3, 2002
posts:18903
votes: 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.
1:08 pm on Jan 19, 2013 (gmt 0)

New User

joined:Jan 19, 2013
posts: 4
votes: 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.