Skip to content

Instantly share code, notes, and snippets.

@zellwk
Last active February 18, 2016 02:57
Show Gist options
  • Save zellwk/e222902ecfb555cfcfc4 to your computer and use it in GitHub Desktop.
Save zellwk/e222902ecfb555cfcfc4 to your computer and use it in GitHub Desktop.
## Redirects I hope to achieve:
- http://zell-weekeat.com/ -> http://zellwk.com/
- http://zell-weekeat.com/about -> http://zellwk.com/about
- http://zell-weekeat.com/about/ -> http://zellwk.com/about
- http://zell-weekeat.com/wp-admin/* -> http://zell-weekeat.com/wp-admin/*
- http://zell-weekeat.com/wp-content/* -> http://zell-weekeat.com/wp-content/*
- http://zell-weekeat.com/* -> http://zellwk.com/blog/*
- http://zell-weekeat.com/*/ -> http://zellwk.com/blog/*
## Present Condition
The present condition is that all urls are routed from zell-weekeat.com/ to zell-weekeat.com /zellwk/
```
- public_html/
|- zellwk/
|- my current files
```
The .htaccess is:
```
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$
RewriteCond %{REQUEST_URI} !^/zellwk/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /zellwk/$1
RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$
```
## What I've tried:
```
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$
RewriteRule about(/)$ http://zellwk.com/about [R=301]
RewriteCond %{REQUEST_URI} !^/zellwk/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /wp-admin/(.*) /zellwk/wp-admin/$1
RewriteRule /wp-content/uploads/(.*) /zellwk/wp-content/uploads/$1 [L]
RewriteRule ^(.*)$ http://zellwk.com/blog/$1
RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$
```
URL redirection failled totally. `/about` went to something like `http://zellwk.com/blog/about` or `http://zellwk.com/blog/http:zellwk.com/about`.
Any help would be greatly appreciated!
@jonsuh
Copy link

jonsuh commented Feb 16, 2016

Try this:

RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$

# http://zell-weekeat.com/about(/) -> http://zellwk.com/about
RewriteRule about/?$ http://zellwk.com/about [R=301]

# http://zell-weekeat.com/(wp-content|wp-admin)/* -> http://zell-weekeat.com/zellwk/(wp-content|admin)/*
RewriteCond %{REQUEST_URI} ^/(wp-content|wp-admin)
RewriteRule ^(.*)$ /zellwk/$1

# http://zell-weekeat.com/* -> http://zellwk.com/blog/*
RewriteCond %{REQUEST_URI} !^/(wp-content|wp-admin)
RewriteRule ^(.*)$ http://zellwk.com/blog/$1 [R=301]

Also a note: If someone goes to http://zell-weekeat.com/blog/hi/, it’ll redirect to http://zellwk.com/blog/blog/hi/ so to account for that, make sure to set rewrite rules on zellwk.com as well.

@zellwk
Copy link
Author

zellwk commented Feb 18, 2016

Here's the solution that worked. Thanks to @simkimsia

RewriteEngine on

RewriteCond %{HTTP_HOST} ^(www.)?zell-weekeat.com$

# when whatever comes after zell-weekeat.com/ matches about OR about/
# go to ...
# the L makes sure once this rule matches, stop checking the rest.
# the 301 is for the redirection status code which is good for SEO
# read https://moz.com/learn/seo/redirection
RewriteRule ^about($|/) http://zellwk.com/about [R=301,L]

# when whatever comes after zell-weekeat.com/ AND does NOT match wp-{wildcard here....}
# take that whatever and append to http://zellwk.com/blog/
# and then redirect there using status code 301 and if this rule is true, stop any further
RewriteRule !^(wp\-.*) http://zellwk.com/blog%{REQUEST_URI} [R=301,L]


RewriteCond %{REQUEST_URI} !^/zellwk/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^(.*)$ /zellwk/$1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment