开发者

RewriteRule: Redirect old paths to archive server and canonicalize to non-www domain?

开发者 https://www.devze.com 2023-02-13 12:19 出处:网络
I am moving the old server to archive.example.com, and the new server will conti开发者_StackOverflownue to run on example.com while all www URLs are canonicalized to either example.com or archive.exam

I am moving the old server to archive.example.com, and the new server will conti开发者_StackOverflownue to run on example.com while all www URLs are canonicalized to either example.com or archive.example.com and should deal with the trailing slash issue.

The old server has many directories so everything needs to redirect to archive.example.com while retaining the path information, except for a few directories which will run on the new server. The directories I do NOT want to redirect and will remain for the new server are:

/ (root)
/static
/blog
/about

For example:

example.com => example.com 
www.example.com => example.com
www.example.com/ => example.com/

example.com/blog => example.com/blog
www.example.com/blog => example.com/blog
www.example.com/blog/ => example.com/blog/

All other directories should redirect to archive.example.com. For example:

example.com/docs => archive.example.com/docs
www.example.com/docs => archive.example.com/docs
www.example.com/docs/ => archive.example.com/docs/

example.com/library/images => archive.example.com/library/images
www.example.com/library/images => archive.example.com/library/images
www.example.com/library/images/ => archive.example.com/library/images/

Here is what I have in my httpd.conf file:


ServerName example.com
ServerAlias www.example.com
UseCanonicalName On

# canonicalize www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ $1 [R=301]

# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^(/|/static|/blog|/about)$
RewriteRule ^/(.*)$ http://archive.example.com/$1  [NC,R=301,L]

Is this correct and/or is there a more precise way?


RewriteEngine On
RewriteCond %{HTTP_HOST} !^gotactics.net$ [NC]
RewriteRule ^(.*)$ http://gotactics.net/$1 [L,R=301]

This will remove all www. Im sure you can change it too do different if needed.


I believe I found my issue -- it was with the RewriteRule that redirected to the old site.

This is what I had when I posted the question:


# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^(/|/static|/blog|/about)$
RewriteRule ^/(.*)$ http://archive.example.com/$1  [NC,R=301,L]

...and I rewrote this to:


# redirect everything to archive.example.com except for a few directories
RewriteCond  %{REQUEST_URI} !^/$
RewriteCond  %{REQUEST_URI} !^/static.*$
RewriteCond  %{REQUEST_URI} !^/blog.*$
RewriteCond  %{REQUEST_URI} !^/about.*$
RewriteRule ^(.*)$ http://archive.example.com%{REQUEST_URI}  [NC,R=301,L]

Here's why.

First, as you can see, I broke up the single rewrite condition into four separate conditions because this will enable me to cleanly add more directories for exclusion as the new site grows.

You will also notice that I added a dot-star after /static, /blog/ and /about so that it will match on any path in those directories and not just the top level.

Finally, on the RewriteRule line I removed the leading slash from the pattern and changed the trailing /$1 to %{REQUEST_URI} . I don't need to store any variables from the pattern here -- I just need to change the server name -- so instead of extracting the path from the pattern, I made it more explicit by using the same %{REQUEST_URI} variable that was used on the previous four lines.

BTW: One of the reasons this was causing confusion for me at first was because Chrome was sometimes caching the DNS/path info -- doing a Ctrl-F5 to purge the cache will enable you to see your changes.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号