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

Apache Web Server Forum

    
Remove query string from dynamic URL
mrgubu




msg:4599808
 6:17 pm on Aug 6, 2013 (gmt 0)

I want to remove the query string (facebook stuff) of a URL , but I want to maintain the rest of URL as it is. The problem is that the rest of the URL is also a redirection that converts a dynamic url into a static URL:

This is a sample of a URL I want to remove the query string from it:

http://www.domain.com/news/1234/the-content-of-the-new?fb_comment_id=1234567890

I want that URL to be converted to:

http://www.domain.com/news/1234/the-content-of-the-new

I put in the following into the .htaccess file:

RewriteCond %{REQUEST_URI} ^/news/.*$
RewriteCond %{QUERY_STRING} ^fb_comment_id.*$
RewriteRule ^(.*)$ $1? [R,L]


But in the .htaccess there is also this rule:

RewriteRule ^news/([^/]+).*$ new.php?id=$1 [L]

So the url converts to:

http://www.domain.com/new.php?id=1234

But I want to maintain this original way:

http://www.domain.com/news/1234/the-content-of-the-new

 

phranque




msg:4599841
 9:22 pm on Aug 6, 2013 (gmt 0)

your redirects should precede the rewrite.

you probably want a 301 redirect and the [R] flag defaults to a 302.
you should also provide a fully qualified url in the Substitution string including protocol and hostname.

since you aren't capturing anything you should get rid of the .*$ in the RewriteCond Patterns.

g1smd




msg:4599849
 10:14 pm on Aug 6, 2013 (gmt 0)

RewriteCond %{QUERY_STRING} ^fb_comment_id
RewriteRule ^(news/.*) http://www.example.com/$1? [R=301,L]


The rule order is important. This should be before any non-www/www canonicalisation rule and that should be before any internal rewrites. If you put the rules in the wrong order, internally rewritten paths are exposed back out on to the web as URLs.

mrgubu




msg:4599924
 8:59 am on Aug 7, 2013 (gmt 0)

OK I solved the problem thanks to the help of phranque and g1smd

The main issue was that I had to write a full url string with protocol and hostname in the rewrite rule.

I'd like to ask a last question: Should I add a R=301 to this rewrite?:

RewriteRule ^news/([^/]+).*$ new.php?id=$1 [L]
lucy24




msg:4599929
 9:17 am on Aug 7, 2013 (gmt 0)

Should I add a R=301 to this rewrite?:

NOOOOO.

Oops. Sorry.

No. You must absolutely not add an [R] flag to any rule that is not meant to be a redirect.

A redirect means "change the the address bar".

A rewrite-- what you've got with the [L] flag alone-- means "leave the address bar alone but fetch the page content from this other place".

Frankly I am more worried about

But in the .htaccess there is also this rule:

RewriteRule ^news/([^/]+).*$ new.php?id=$1 [L]

So the url converts to:

http://www.example.com/new.php?id=1234

The rule you quote is a rewrite without redirect. If it leads to a change in URL, the rewrite is getting mixed up with a redirect somewhere else. What should happen with the quoted rule is:

Request comes in for
^news/blahblah/and-possibly-more-stuff

Server quietly goes off and asks the page /new.php to provide content based on the value of "blahblah". User sees this content, but address bar continues to say
example.com/news/more-stuff-here

The main issue was that I had to write a full url string with protocol and hostname in the rewrite rule.

Include the protocol and hostname-- and the [R=301] flag --if you want the address bar to change. Otherwise, use only a / leading slash with [L] flag.

mrgubu




msg:4599930
 9:39 am on Aug 7, 2013 (gmt 0)

Thank you lucy24, I think I understand you.

Finally the code in my htaccess stay as:

RewriteCond %{QUERY_STRING} ^fb_comment_id
RewriteRule ^(news/.*) http://www.domain.com/$1? [R=301,L]

RewriteRule ^news/([^/]+).*$ new.php?id=$1 [L]


So any url like

http://www.domain.com/news/1234/the-content-of-the-new?fb_comment_id=1234567890

is converted to

http://www.domain.com/news/1234/the-content-of-the-new

without any more problems.

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.
Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About
© Webmaster World 1996-2014 all rights reserved