htaccess redirect files to new domain - regex

I need to direct all files on one domain to the homepage of the new domain (I have to due to a new structure). I seem unable to do this, so for example I use the below code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^olddomain\.co\.uk$ [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk [R=301,L]
But if I have the below link:
www.olddomain.co.uk/shop/product_info.php?cPath=592&products_id=9335 it does not direct as I want it to, I want it to direct to the homepage of www.newdomain.co.uk but it goes to:
http://www.newdomain.co.uk/?cPath=592&products_id=9335
What do I need to do to amend the htacess on the old domain?
Thanks

You can use this rule:
RewriteCond %{QUERY_STRING} ^cpath= [NC]
RewriteRule ^/?$ /? [R=301,L]
/? in target will strip off original query string.

Related

301 redirect.htaccess on entire site EXCEPT for main domain url

I all, I've been searching a lot but I don't find an answer to my problem.
I have already made a .htaccess file that redirects all the different pages and files on my old domain (www.olddomain.com) to my new domain (www.newdomain/blog.com). I'm moving a blog to an e-commerce, that's why 301 to www.newdomain/blog.com.
However, I need to use the Change of Address Tool (Google Webmaster Tools) but I can't because www.olddomain.com must redirect to www.newdomain.com and now it is redirecting to www.newdomain/blog.com and Google don't let me change the address.
My .htaccess is working fine except for that, here it is
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule (.*)$ http:/www.newdomain.com/blog/$1 [R=301,L]
BUT, I have not found any way to make an exception to redirect just www.olddomain.com must ---> www.newdomain.com
I have tried to make that exception in the following way:
RewriteCond %{HTTP_HOST} ^olddomain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.olddomain\.com$
RewriteRule ^/$ http://www.newdomain.com/ [R=301,L]
RewriteRule (.*)$ http://www.newdomain.com/blog/$1 [R=301,L]
But nothing has worked...
Please, can you help me?
Thanks in advance
Try :
RewriteEngine On
#redirect http://oldsite.com/ to http://newsite.com
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^$ http:/www.newdomain.com/$1 [R=301,L]
#redirect http://oldsite.com/pages to http://newsite.com/blog/pages
RewriteCond %{HTTP_HOST} ^(www\.)?olddomain\.com$
RewriteRule ^(.+)$ http:/www.newdomain.com/blog/$1 [R=301,L]
Note the ^$ in pattern it matches / ie : http://example.com/ or the directory the htaccess file is in.
Clear your browser cache before testing this.

htacces phpbb to xenforo

i am trying to redirect thoses urls ( from a phphbb )
http://www.example.com/fraiseuses/restauration-gambin-10n-t33924-45.html
http://www.example.com/fraiseuses/une-gambin-10n-plus-sur-forum-t34642.html#p675680
to a the correct xenforo url
http://www.example.com/threads/restauration-gambin-10n.33924/page-3
http://www.example.com/threads/34642/page-1#post-675680
my htacces
RewriteEngine On
RewriteRule ^post([0-9]+).html$ /posts/$1 [R=301,L]
RewriteRule ^.*-f([0-9]+)$ /forums/$1 [R=301,L]
RewriteRule ^[^/]+/topic([0-9]+).html /threads/$1 [R=301,L]
RewriteRule ^.*-t([0-9]+).html /threads/$1 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-15\.html$ /threads/$1/page-1 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-30\.html$ /threads/$1/page-2 [R=301,L]
RewriteRule ^[^/]+/[^\./]+-t([0-9]+)-45\.html$ /threads/$1/page-3 [R=301,L]
Unfortunately i can't find my mistake, can somebody point out my mistake please? Thanks.
There are no phpBB-specific redirect scripts, but these will work::
http://xenforo.com/community/threads/redirection-scripts-for-vbulletin-3-x.5030/
The basic function of these scripts is to map the old ids to the new ones. They can work with phpBB URLs given appropriate rewrite rules. Here are steps for you:
1) Upload these redirect scripts to your old forum location (the /forum folder):
http://xenforo.com/community/threads/redirection-scripts-for-vbulletin-3-x.5030/
You only need the 301config.php file and the showthread.php file to redirect threads (which are the most important thing).
2) Edit the 301config.php file. Uncomment this line (remove the //) and specify the path to your XF directory:
// $fileDir = '/home/username/www/forums';
You may also need to specify the name of the import log table if it's not the default (xf_import_log). It may be named archived_import_log:
// define('IMPORT_LOG_TABLE', 'import_log_x');
3) Add these rewrite rules to your .htaccess file in the /forum directory. This is based on previous phpBB imports. I assume your URLs follow the same format:
RewriteEngine on
RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$)
RewriteRule ^viewtopic\.php$ /forum/showthread.php?t=%2 [L,R=301,NC]
thx
i did this :
RewriteCond %{QUERY_STRING} (^|&)t=([0-9]+)(&|$) [NC]
RewriteRule ^viewtopic\.php$ /threads/%2? [L,R=301,NC]
RewriteCond %{QUERY_STRING} (^|&)p=([0-9]+)(&|$) [NC]
RewriteRule ^viewtopic\.php$ /posts/%2? [L,R=301,NC]
RewriteCond %{QUERY_STRING} f=(\d+)$ [NC]
RewriteRule ^viewforum\.php$ /forums/%1 [L,R=301,NC]
RewriteRule ^viewforum\.php$ /forums/%1? [L,R=301,NC]

How to rewrite rules in htacess file

I want to write rules to redirect to following pages using .htaccess file.I am new to this.
How I suppose to do it?
Facebook: test.com/FB (redirects to https://www.facebook.com/test.etutors)
Twitter: test.com/Twitter (redirects to https://twitter.com/test)
Your attempted rule won't work because RewriteRule only matches request URI not the domain part. Have your rules like this in your root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^FB/?$ https://www.facebook.com/test.etutors [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)test\.com$ [NC]
RewriteRule ^Twitter/?$ https://twitter.com/test [L,NC,R=301]
PS: Since you're a new user I suggest you to show your code by editing your question rather than in comments.

.htaccess redirect with parameters and multiple directory levels

I need to redirect from this URL:
www.devsite.com/level1/page1.html?brand=6
to:
www.productionsite.com/level1/page1.html?brand=6
I've come across various redirect w/ parameters answers here on stack, but none that specifically address how to rewrite part of .htaccess to redirect to a totally different domain. Help!
Just do a redirect from devsite to productionsite. Second line appends after the devsite domain to the new location.
RewriteCond %{HTTP_HOST} !^www.devsite.com$ [NC]
RewriteRule ^(.*)$ http://www.productionsite.com/$1 [L,R=301]
Add this to the htaccess file in the document root of your www.devsite.com domain. Add it above any other rules that may already be in that file.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^(www\.)?devsite\.com$ [NC]
RewriteRule ^level1/page1\.html$ http://www.productionsite.com/level1/page1.html [L,QSA]
RewriteCond %{QUERY_STRING} \?brand=6
RewriteCond %{HTTP_HOST} ^www\.devsite\.com$
RewriteCond %{SERVER_PORT}s ^(443(s)|\d+s)$
RewriteRule ^level1/page1\.html$ http%2://www.productionsite.com/$0 [R=301,QSA,L]

htaccess: redirect old domain and all pages to a new domain

I know that there is a lot of examples on Stackoverflow but I still miss something.
I'm trying to redirect http://old.domain.com/fr/ to http://brand.new-domain.com/fr/ with the following rules, but that doesn't work:
# Enable Rewrite Engine
RewriteEngine On
RewriteBase /
# Add a trailing slash to paths without an extension
RewriteCond %{REQUEST_METHOD} !=POST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [r=301,L]
# Remove index.php
# Uses the "exclude method"
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Exclude_List_Method
# This method seems to work best for us, you might also use the include method.
# http://expressionengine.com/wiki/Remove_index.php_From_URLs/#Include_List_Method
# Exclude root files
RewriteCond $1 !^(index\.php) [NC]
# Exclude EE folders
RewriteCond $1 !^(assets|ee-admin|images|templates|themes|fr|nl)/ [NC]
# Exclude user created folders
RewriteCond $1 !^(assets|css|img|js|swf|uploads)/ [NC]
# Exlude favico, robots, ipad icon
RewriteCond $1 !^(favicon\.ico|robots\.txt|pple-touch-icon\.png) [NC]
# Remove index.php
RewriteCond %{QUERY_STRING} !^(ACT=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(URL=.*)$ [NC]
RewriteRule ^(.*)$ /index.php?/$1 [L]
It correctly redirect when I call the root URL, but not when I call a page. What am I doing wrong?
Thanks in advance!
Pv
When writing mod_rewrite rules, the rules get applied in the order that they appear.
To redirect an old domain to a new domain, you'll want that rule to be first in your .htaccess or httpd.conf file — all other rules should appear after it.
If you only want to redirect a certain directory, the following rule will do so, while allowing the rest of the site to function normally:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Only Matching Directories
RewriteCond %{REQUEST_URI} ^/(fr|fr/.*)$
RewriteRule ^(.*)$ http://brand.new-domain.com/fr/$1 [R=301,L]
</IfModule>
If you want to redirect the entire site, the following rule will do so:
<IfModule mod_rewrite.c>
RewriteEngine On
# Redirect Entire Site to New Domain
RewriteCond %{HTTP_HOST} ^old.domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com$ [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
</IfModule>
If you care about letting crawlers know your content has moved and want to make the transition as seamless as possible, be sure to keep the 301 Redirect flag in the RewriteRule.
This will ensure that users and search engines are directed to the correct page.
While we're on the subject, as part of the EE 2.2 release, EllisLab now "officially" offers limited technical support for removing index.php from ExpressionEngine URLs.
Simply add or update your code to the following, making sure to consider any rules you may already have in place:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Try to use the following ruke as the first one:
# Redirect domain
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^old.domain.com [OR]
RewriteCond %{HTTP_HOST} ^other-old.domain.com [NC]
RewriteRule ^(.*)$ http://brand.new-domain.com/$1 [R=301,L]
Also mind the upper case R with is the short form for the lower case redirect.
Have you tried using mod_alias simple redirect instructions (a core module that you have), before trying the hacky-mod-rewrite thing?
I would do a VirtualHost with ServerName old.domain.com and in this VH I would add this rule:
Redirect /fr http://brand.new-domain.com/fr
from doc:
Then any request beginning with URL-Path will return a redirect request to the client at the location of the target URL. Additional path information beyond the matched URL-Path will be appended to the target URL.
So get a separate VirtualHost for brand.new-domain.com (with ServerName brand.new-domain.com) and in this one do not set the Redirect Rule.
If you still want to handle the 2 domains in the same VirtualHost then you'll have to use mod-rewrite as even RedirectMatch cannot check the request domain on the query.