Forum Moderators: phranque
So, we'd like
http://www.example.org/blog/donate/donate-now
http://www.example.org/blog/donate/donate-now/
http://example.org/blog/donate/donate-now
http://example.org/blog/donate/donate-now/
all to be redirected to:
[example.org...]
I can use the server config file or the htaccess file, but have been using the htaccess file, which also has the wordpress code for pretty permalinks:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I think that my confusion is being caused by the redirection that wp is already doing. ie. there is no /blog/donate/donate-now structure in the filesystem. So I'm not sure how to set up my conditions.
Thanks so much in advance for anyone who can help me figure out what I'm sure is a simple problem.
[edited by: jdMorgan at 8:45 pm (utc) on June 10, 2009]
[edit reason] example.org [/edit]
So for any given page of content, sixteen URLs will compete for search ranking, and the rank of each of those actual pages may be one-sixteenth what it could be... Then there's the problem that the search engines --and not you-- will pick the URL that "they like best" for whatever reason.
If you have no other domains or subdomains associated with this site, then use this in example.org/.htaccess:
RewriteCond %{HTTP_HOST} !^www\.example\.org$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+)$
RewriteRule ^(.*)$ http%2://www.example.org/$1 [R=301,L]
If you do have other domains or additional subdomains, then the code will be more complex, and we'll need to discuss the details here.
To address the trailing slash problem, first make sure that WP is correctly generating all on-page links with the trailing slash. You can then fix-up linking errors on third-party sites using this in example.org/.htaccess:
RewriteRule ^(([^/]+/)*[^./]*[^/])$ http://www.example.org/$1/ [R=301,L]
Your best bet would be to fix the https problem by linking to the page using the proper protocol from all pages within your own site, and only then installing a redirect. Otherwise, you confuse the search engines, and force all clients (browsers and SE robots) to make two HTTP requests for that page -- The first for http/xyz/donate will result in the server responding with a redirect, and the client will then have to re-request the page using the [xyz...] URL. This slows the user experience, and pollutes your logs and stats with two request/response transactions every time that page is requested. Fix the links if it is at all possible.
Then you can add this to example.org/.htaccess:
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^blog/donate/donate-now/?$ https://www.example.org/blog/donate/donate-now/ [R=301,L]
And once again, for use in a server config file outside of a <Directory> container, you'll need to add a leading slash to the RewriteRule pattern.
---
So the whole pile, properly ordered, will look like this in example.org/.htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
#
# Redirect to force SSL and trailing slash on bad links to donate-now page
RewriteCond %{SERVER_PORT} !=443 [OR]
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^blog/donate/donate-now/?$ https://www.example.org/blog/donate/donate-now/ [R=301,L]
#
# Redirect to add missing trailing slash unless URL ends with a "filetype"
RewriteRule ^(([^/]+/)*[^./]*[^/])$ http://www.example.org/$1/ [R=301,L]
#
# Redirect all non-canonical hostname requests to canonical domain, preserving SSL/non-SSL
RewriteCond %{HTTP_HOST} !^www\.example\.org$
RewriteCond %{SERVER_PORT}s ^(443(s)¦[0-9]+)$
RewriteRule ^(.*)$ http%2://www.example.org/$1 [R=301,L]
Note that the rules are ordered from most-specific pattern and conditions to least-specific in order to avoid 'stacked' or 'chained' multiple redirects.
Jim