301 Redirects (htaccess) with exclude conditions - .htaccess

I'm currently moving a site (shop) to a new domain and putting in its place a non-shop version (made in wordpress) of the site. The aim is for most of the urls to redirect to the new domain except the few pages that the new site has. I've found other posts on stackoverflow but unfortunately I can't seem to get it working. Any help would be much appreciated, Thanks Jason.
The urls I need to exclude are:
/ (Homepage)
/news and its posts /news/post-name
/products and its posts /products/post-name
/offers and its posts /offers/post-name
/our-philosophy
/our-team
/our-dream
Here's what I tried:
<IfModule mod_rewrite.c>
RewriteEngine on
#Home
RewriteCond %{REQUEST_URI} !^/
#News
RewriteCond %{REQUEST_URI} !^/news(.*)
#Products
RewriteCond %{REQUEST_URI} !^/products(.*)
#Offers
RewriteCond %{REQUEST_URI} !^/offers(.*)
#Philosophy
RewriteCond %{REQUEST_URI} !^/our-philosophy(.*)
#Team
RewriteCond %{REQUEST_URI} !^/our-team(.*)
#Dream
RewriteCond %{REQUEST_URI} !^/our-dream(.*)
RewriteRule (.*) http://www.cotswold-fayre.co.uk/$1 [R=301,L]
</IfModule>
# 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

try adding the following to your .htaccess file
RewriteEngine on
#Home: Exclude the home Page
RewriteCond %{REQUEST_URI} !^/$ [OR]
#News: exclude anything that starts with /news, /products etc
RewriteCond %{REQUEST_URI} !^/(news|products|offers|our-philosophy|our-team|our-dream) [NC]
RewriteRule (.*) http://www.cotswold-fayre.co.uk/$1 [R=301,L]

Related

.htaccess: rewrite to new URL structure

I'm trying to have the following URL redirect to a new structure:
Before: http://example.com/profile/?name=some-one
After: http://example.com/profile/some-one/
The some-name will change depending on the user's account, so I want that part to be dynamic. This is what I have for my .htaccess
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{QUERY_STRING} (^|&)name=(.*)
RewriteRule ^profile/$ /profile/$1/? [L,R=301]
According to this online htaccess tester, this would be the result:
http://example.com/profile//
Any help on clarifying why it's not carrying over the dynamic some-name? If it matters, this is for a WordPress site which already has the following:
# 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
Try
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteCond %{QUERY_STRING} (^|&)name=(.*)
RewriteRule ^(.*)$ /profile/%2/? [R=301,L]
Use % for vars captured in RewriteCond's

HTACCESS redirect all old domain urls to new domain

I am new to HTACCESS and have a very specific question regarding 301 redirects. I am looking to redirect all .co.uk urls to .co but also I want to redirect all suburls to the root URL so for example I want
http://virtualfiles.co.uk/scenarios.html -> http://virtualfiles.co
A lot of these pages don't exist anymore so I would rather just relocate the user to the top level domain rather than a page that doesn't exist on the old or new domain.
I am set up on WordPress and the code I have so far is below. Any help would be amazing.
# 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
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*) index.php [NC]
You can try this in your domain1/.htaccess :
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?virtualfiles\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://virtualfiles.co/$1 [R=302,L]
Or Try With RedirectMatch
RedirectMatch 301 ^/(.*)$ http://virtualfiles.co/$1

.htaccess Redirect rule to another domain

Currently I can rewrite a URL like this
RewriteRule ^pageOne$ /pageOne.php
I would like to do a Redirect to another domain not a rewrite to the current
So in the example below users who click on pageOne get redirected to the other URL
RewriteRule ^www.example.com/pageOne$ www.newexample.com/pageOne.php
EDITED
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^blog.example.co.uk$
RewriteRule ^a-blog-post$ http://www.newexample.com/reviced-blog-post.html [L,R=301,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
That rule will be like this:
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^pageOne/?$ http://www.newexample.com/pageOne.php [L,R=301,NC]

.htaccess retrieve from folder if subdomain, not 301 redirecting?

I want my site to just point to /user folder if the request is a subdomain.
If the request is subdomain.site.com/admin, then the site should show the page for subdomain.site.com/user/admin.
The problem with my code is that it makes an 301 redirect instead of just keeping the url-address.
My code look like this:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se(.*)
RewriteCond %{HTTP_HOST} ^[^.]+.bildsida.se(.*)
RewriteRule ^$ user/$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) user/$1 [L]
</IfModule>
And you can try for yourself, go to http://mickes.bildsida.se/admin and see how the address changes to /user/admin. Very disturbing...
You just need a few of the lines you showed to get this working.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} ^.+\.bildsida\.se
RewriteRule ^(.*)$ user/$1 [L]
I adjusted the HTTP_HOST checks because that parameter only looks at the domain name, so you don't need the (.*) at the end. I also removed the checks that look if the file exists or is a directory, since you want everything redirected (no reason to make it possible to access files from other subdomains, for example)
You code can be simplified to these lines : (try them instead)
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.)?bildsida\.se [NC]
RewriteRule ^(.*)$ /user/$1 [QSA,L]
Finally! After days and nights of reading forums and documentations, and testing all possible ways, I got it to work!
Sulotion:
<IfModule mod_rewrite.c>
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.bildsida.se
RewriteCond %{HTTP_HOST} !^bildsida.se
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ user/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)? user/$1/ [QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule /(.+)/ user/$1 [QSA]
</IfModule>

.htaccess redirect woes — not completing

So, at the moment, my .htaccess looks a little like this:
# 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
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ "http\:\/\/i\.colorspace\.am\/portfolio$1" [R=301,L]
I want to move all the content from my root directory into a subdomain (which I've done) but I don't want the links people have to not work. www.colorspace.am/portfolio needs to redirect to i.colorspace.am/portfolio, and all the sets contained therein (ie /portfolio/YYYYMMDD) also need to be 'adjusted' on the fly (www.colorspace.am/portfolio/YYYYMMDD -> i.colorspace.am/porfolio/YYYYMMDD
NOTE: i.colorspace.am contain's 2011's content; ii.colorspace.am will contain 2012. They're two entirely different WP installs with their own respective databases. Not sure if it's relevant but..
What seems to be happening is that /portfolio is instructed to redirect to i.colorspace.am/portfolio, but for whatever reason it's ending up at i.colorspace.am
IF there's a way I can make any www.colorspace.am/folder/sub-folder redirect to i.colorspace.am/folder/sub-folder (wildcard?) ..that would be amazing. But I'd be just as happy with a single fully working redirect at this point.
NOTE: the redirect code was generated by my administration panel. I tried
Redirect /portfolio http://i.colorspace.am/portfolio
But it resulted in a 'too many redirects' error.
After the redirection from
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
The rules
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Is accessed again. That is the reason it's ending up at i.colorspace.am.
Add these 2 rules
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
in order mentioned below.
also change (just a small optimization)
RewriteCond %{HTTP_HOST} ^colorspace\.am$ [OR]
RewriteCond %{HTTP_HOST} ^www\.colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
to
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
and put it inside of IfModule block in the same order mentioned below.
Have just the below in your .htaccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#add these 2 lines:
RewriteCond %{HTTP_HOST} ^(?:www\.)?i.colorspace\.am$
RewriteRule protfolio -[L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?colorspace\.am$
RewriteRule ^portfolio\/?(.*)$ http//i.colorspace.am/portfolio$1 [R=301,L]
</IfModule>
# END WordPress

Resources