homepage Welcome to WebmasterWorld Guest from 54.235.16.159
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
Rewriting Canonical and Dynamic The Right Way?
How do I write the rewrite the right way?
underglass




msg:4337177
 10:05 pm on Jul 8, 2011 (gmt 0)

Hi,

This is directed to g1smd and lucy24 [webmasterworld.com...] but anyone can help. I have a WordPress e-commerce site. I thought my canonical no-www to www was correct by g1msd says
It fails to redirect www requests with appended port numbers

Here is is again

RewriteEngine On
RewriteCond %{HTTP_HOST} ^mywidgets.com
RewriteRule (.*) [mywidgets.com...] [R=301,L]

How would I correct this?

lucy24,

Are you trying to get rid of the queries entirely, or just change to different ones?

I want to get rid of these queries entirely. Yahoo and Bing are placing them in their results.

These are not "real" pages on my site. They are different URLs for the same page - my homepage. I cannot figure out how the keyword (kw) is being used to create the URL (?kw=round yellow widgets) since I have no keyword plugins.

How would I write this using to put %{QUERY_STRING} as a RewriteCond as you suggest?

The URL looks like this www.mywidgets.com/?kw=round yellow widgets

I want to get rid of everything after the / that is dynamically created.

I am using hostgator

 

g1smd




msg:4337181
 10:08 pm on Jul 8, 2011 (gmt 0)

For non-www/www redirect use this pattern:
!^(www\.example\.com)?$ instead.

Use example.com here to stop forum auto-linking

I want to get rid of everything after the / that is dynamically created.

After the slash or after the question mark? Slash would be removing "path" elements. Question mark would be "query string".

underglass




msg:4337201
 10:42 pm on Jul 8, 2011 (gmt 0)

For non-www/www redirect use this pattern: !^(www\.example\.com)?$ instead.

Thank you! Will try and let you know.

After the slash or after the question mark? Slash would be removing "path" elements. Question mark would be "query string".

The query string - the question mark and everything after.

I want to keep the path.

g1smd




msg:4337225
 11:08 pm on Jul 8, 2011 (gmt 0)

A question mark on the end of the target removes any and all parameters.

You'll need a RewriteCond testing QUERY_STRING or THE_REQUEST to make sure the original request did have an appended query string. To be clear, if you accidentally redirect "without query string" to "without query string", you will end up with an infinite redirect loop.

underglass




msg:4337255
 12:31 am on Jul 9, 2011 (gmt 0)

I may be completely misunderstanding you, but will this do both the non-www/www redirect, remove any and all parameters, and not create an infinite redirect loop? (the +++ are mine - do not want to create a false link)

RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) +h+t+t+p://www.example.com/$1 [R=301,L]

# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

# END WordPress

This is all I have in my .htaccess file. I really have zero experience with this. Zip. Nada.

g1smd




msg:4337257
 12:35 am on Jul 9, 2011 (gmt 0)

You need to read a few tutorials on mod_rewrite and on Regular Expressions.

The existing rule redirects any hostname request that is not exactly "www.example.com" to "www.example.com". The page detail is unchanged by the redirect.

You need additional code, maybe even additional rules, to cater for the other things as described above.

underglass




msg:4337263
 12:45 am on Jul 9, 2011 (gmt 0)

I will read a few tutorials. I made none of the suggested changes to my .htaccess file. I have had the same one in place for many months and does not seem to be causing any major problems with ranking or my site.

Thank you just the same!

lucy24




msg:4337274
 2:09 am on Jul 9, 2011 (gmt 0)

will this do both the non-www/www redirect, remove any and all parameters, and not create an infinite redirect loop? (the +++ are mine - do not want to create a false link)

Heheh, that's the whole point of example.com. The Forums are especially coded so http://www.example.com will never turn into a link:
http://www.example.com
http://www.example.com/
http://www.example.com/index.html
http://www.example.com/index.php
http://www.example.com/folder1/folder2/folder3/filename.html
Wasn't that fun?

You'll need a RewriteCond testing QUERY_STRING or THE_REQUEST to make sure the original request did have an appended query string. To be clear, if you accidentally redirect "without query string" to "without query string", you will end up with an infinite redirect loop.

That is: At every point, you need to make sure the rules only apply to patterns that have not already had this Rewrite done to them. (That is what makes [N] so dangerous. You have to make sure that eventually there will be nothing left to rewrite.)

In your case, there are two things:

!^(www\.example\.com)?$
means apply the following rule to any request that doesn't use either the exact name "www.example.com" or nothing at all.

%{QUERY_STRING} \S
means apply the following rule only to requests that currently have a query string. \S means a non-space of any kind, un-anchored. You might even get away with . meaning "anything whatsoever" but that looks a bit too minimalist. If your Mystery Code is appending spurious queries that consist entirely of spaces, I don't want to hear about it.

g1smd




msg:4337312
 6:44 am on Jul 9, 2011 (gmt 0)

Yes, this is what you need:

{QUERY_STRING} .

The " . " is important.

underglass




msg:4337384
 2:09 pm on Jul 9, 2011 (gmt 0)

http://www.example.com
http://www.example.com/
http://www.example.com/index.html
http://www.example.com/index.php
http://www.example.com/folder1/folder2/folder3/filename.html
Wasn't that fun?

Yes, that is fun...and thank you!

After reading some tutorials, I think what you both wrote will make sense. Or at least I hope so. I "get" what you are saying, but do not yet know how to write it without causing some problems.

Right now I think the quickest way to solve my issue is to use the

<link rel="canonical" href="http://www.example.com/"/> and my WP redirection plugin when necessary.

Thank you both for your help.

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