Redirect all to base domain - regex

I would like to redirect all pages like:
www.mydomain.com/test
www.mydomain.com/test2/test3
and so on ...
to always base
www.mydomain.com
How can i do this?
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{THE_REQUEST} ^/(.*)$
RewriteRule (.*) http://%{HTTP_HOST} [L,QSA,R=301]
wont work

RedirectMatch 301 ^/ http://www.mydomain.com/
It will redirect everything to your new domain.
This will work if you have mod_alias

Please try this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([a-z].*)
RewriteRule .* http://kap.com/ [R,L]

I find that solution OK
RewriteCond %{HTTP_HOST} ^www.mydomain.com
RewriteCond %{REQUEST_URI} ^/[a-zA-Z0-9\/]+$
RewriteRule ^ / [R,L]

Just leave out the RewriteConds and redirect everything to /
RewriteCond %{REQUEST_URI} !^/$
RewriteRule ^ / [R,L]
When everything works as you expect, you can change R to R=301.
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Related

I'm having an issue with my .htaccess redirection

I need to use this url http://yoursite.com/proprietes.php?viewid&mls=[18348939] but I wan it to go on a pdf a-very-new-post.pdf. I want to do a redirection in the htaccess. It is not working... I'm in wordpress
thanks!
RewriteEngine On
Redirect 301 /proprietes.php?viewid&mls=[18348939]$ http://yoursite.com/a-very-new-post.pdf
Redirect or RewriteRule directives cannot match query string. Use a RewriteCond instead like this:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post.pdf? [L,B,R=301]
RewriteCond %{THE_REQUEST} \s/en/proprietes\.php\?viewid&mls=\[?18348939\]? [NC]
RewriteRule ^ /a-very-new-post-english.pdf? [L,B,R=301]

Redirect 301 with params

I am redirecting traffic between old web and new web and I need to redirect 301 from /hotel.php?hotel=lasvegas&lng=es to http://www.new-web.com/hotel-las-vegas/
How could be possible from htaccess?
I have tried
Rewriterule ^hotel\.php\?hotel=lasvegas\&lng=es http://www.new-web.com/hotel-las-vegas/ [L,R=301]
I think my error is at regular expression. Can anyone help me?
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /hotel\.php\?hotel=lasvegas&lng=es [NC]
RewriteRule ^ hotel-las-begas? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^hotel-las-vegas/?$ hotel.php?hotel=lasvegas&lng=es [L,QSA,NC]
it's very important that you insert best code of redirect 301 in best place of htaccess. for example :
http://www.abartazeha.com/
we replace abartazeha.com instead of abartazeha.ir
...
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.olddomain.com [NC]
RewriteRule ^(.*)$ http://newdomain.com/$1 [L,R=301]

Apache Rewrite Issue. Unable to figure out query string

So I have been struggling on finding the rule to match this rewrite. I am working on a client website and it is a nightmare with the number of duplicate title tags. I have managed to resolve most of them by enforcing forward slash, redirect non www. to the www. version and disallow crawling of https version of the website.
The issue I am having at the moment. I have over 1000 URLs that are duplicate content, each product has two different URLs with the exact same content. An example is:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
http://www.example.co.uk/product/widget2/
http://www.example.co.uk/widget2/
Now the following URLs have the same content:
http://www.example.co.uk/product/widget1/
http://www.example.co.uk/widget1/
I want to redirect any URL that contains "/product/" to the URL version without "/product/" in the URL if that makes sense. I honestly don't know where to start and would really appreciate the help.
Thanks in advance
EDIT: The recommended rule:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
does not work. It may be conflicting. These are the other rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^/product/(.*)$ /$1 [R=301]
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/])$ /$1$2/ [L,R=301]
I dont know if there are any conflicts here. Please help
Have your full .htaccess like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.co [NC]
RewriteRule (.*) http://www.example.co.uk/$1? [L,R=301]
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?[^/])$ /$1/ [L,R=301]
RewriteRule ^product/([^/]+)/?$ /$1/ [R=301,L]
Assuming the URLs always start with product, this should work:
RewriteEngine On
RewriteRule ^/product/(.*)$ /$1 [R=301]
It'll need to go in your main site conf or .htaccess

How to handle both /url and /url/ redirects with htaccess?

I am rewriting all URLs and shorthand some of them:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(([^\.]+))\.pl [NC]
RewriteRule ^(.*)$ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test$ test-page.php [R=301,L]
so that
example.pl/test redirects to example.pl/test-page.php
but
example.pl/test/ redirects to example.pl/
while it should:
example.pl/test/ redirects to example.pl/test-page.php
How to handle both /url and /url/ redirects?
I assume that it has to something with trailing slash.
You need to make trailing slash optional using /?$ regex. Try these rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^.]+)\.pl [NC]
RewriteRule ^ http://%1.pl%{REQUEST_URI} [R=301,L]
RewriteRule ^test/?$ test-page.php [NC,R=301,L]
Make sure to test this in a new browser to avoid 301 caching issues.
Try this:
RewriteEngine On
RewriteRule ^(.*)/$ http://%{HTTP_HOST}/$1
RewriteRule ^test$ test-page.php [R=301,L]
Tested here.

.htaccess how to redirect subdomain/folder to subdomain only

I'm trying to find an answer on different places, but I can't find the full solution for this.
I had a folder setup for a blog. Now we move to an subdomain setup. The redirect shown below works perfectly. The only thing that doesn't work is when the url is a subdomain combined with folder. that isn't redirected.
RewriteCond %{HTTP_HOST} ^(www\.)domain\.com$
RewriteRule ^blog/(.*)$ http://blog.domain.com/$1 [L,R=301,QSA]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteCond %{REQUEST_URI} !blog/
RewriteRule ^(.*)$ blog/$1 [L,QSA]
www.domain.com/blog/some-url-here is redirected to
blog.domain.com/some-url-here
When I try blog.domain.com/blog/some-url-here it returns a http-status of 200 and when I try to redirect I get a infinitive loop.
Is there a redirect that I've missed?
Try these rules on top of your other rules:
RewriteCond %{THE_REQUEST} \s/blog/([^\s]*) [NC]
RewriteRule ^ http://blog.domain.com/%1 [L,R=301]
RewriteCond %{HTTP_HOST} ^blog\.domain\.com$
RewriteRule !^blog/ blog%{REQUEST_URI} [L,NC]