Using htaccess to strip domain name from URL - .htaccess

I'm not even sure if this is possible but have been looking everywhere for a solution. My site generates dynamic info about domain names and people can view their info in the following format:
http://www.mysite.com/analyze/yourdomain.com
The problem is that sometimes people will enter http:// or www or even the full URL instead of their domain, such as this:
http://www.mysite.com/analyze/http://www.yourdomain.com/this-is-a-test
What I'd like to do with htaccess is get the domain name and redirect it to the proper URL like the first one listed.
This is what I currently have for my htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com$
RewriteCond %{REQUEST_URI} !\.(xml|txt)$
RewriteRule ^(.*)$ http://www.mysite.com/review/$1 [R=301,L]
Options -MultiViews +FollowSymlinks -Indexes
RewriteEngine on
RewriteCond %{REQUEST_URI} !\.(xml|txt)$
RewriteRule ^([a-zA-Z0-9\.\-]+)$ review.php?site=$1 [L,QSA]
The main issue that I'm having is that every tutorial I find about stripping/redirecting domains is applying to my actual domain name, not the user's in the URL. Any help would be greatly appreciated!

Try adding thie right below your first rule (that 301 redirects):
RewriteRule ^analyze/http:/+([^/]+)/? /analyze/$1 [L,R=301]
This will redirect requests like http://www.mysite.com/analyze/http://www.yourdomain.com/this-is-a-test to http://www.mysite.com/analyze/yourdomain.com.

Related

one htaccess for multiple domain, redirect problem

I have multiple domains and one htaccess for these. We split off some content from domain1.com to domain2.com and I need to redirect to the new page.
Redirect 301 /folder/page.html https://www.domain2.com/folder/page.html
resulted in a infinity loop ... i also tried this but its dont work:
RewriteCond %{HTTP_HOST} ^domain1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain1.com$
RewriteRule ^/folder/page.html https://www.domain2.com/folder/page.html [R=301,L]
I also need to mention that not all pages from domain1 should redirect to domain2 but it would be ok if the whole folder is redirected
my knowledge of htaccess isnt that big hope you can help me!
Based on your shown samples, could you please try following. Please place this to the top of your htaccess file.
Also please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteRule ^folder/page\.html https://www.domain2.com/folder/page.html [R=301,NC,L]

.htaccess redirect everything from directory to a different domain

We have a WordPress site/single app which serves two domain names: example.com and example.es.
In our .htaccess file we have currently have the following rules:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.es
RewriteRule ^(.*)$ http://www.exmaple.es/$1 [R=permanent,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
We also need to redirect everything from example.com/es/... to example.es/.... I have tried multiple examples that I could find, but none of them worked (ended up in 500 server error and empty log), here are a few of them:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) http://www.example.es/%1 [R=302,NC]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com/es [NC]
RewriteRule ^(.*)$ http://www.example.es/$1 [R=301,NC,L]
All of the new rules were placed inside the <IfModule mod_rewrite.c>
Any help or guidance is much appreciated.
#Domas Hi Domas, the process in your case is to update you site installing some important extension about the domain change.
Changing the preferred domain name alias WordPress uses
If your WordPress site has a domain name alias pointing to it, and you want WordPress to prefer the alias domain name in links that it generates, you can update the domain name in the WordPress dashboard:
Click Settings.
Change the WordPress Address (URL) to the new URL that you want to use.
Change the Site Address (URL) to the same value.
Click Save Changes.
You need WP Super Cache, this extension help you to update automatically the .htaccess and change all the needs as you did on you configuration site.
First install the extension https://support.tigertech.net/move-wordpress#updating-htaccess-wp-jmp
Also if you don't need any extension and you want to do by the hand just redirect like this example as needs :
#the simple redirection
Redirect 301 /es http://www.example.es/
#Redirect from old domain with subdirectory to new domain
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/es/(.*)$
RewriteRule ^(.*) http://www.example.es%{REQUEST_URI} [R=302,NC]
It depend of you needs i prefer the last one. Regards

catch22: Needing to remove redirect chain I seem to need

I've been using paths like
<?php include($_SERVER['DOCUMENT_ROOT'].'/directory/file.php'); ?>
in my pages, and it works as expected. However, when I run speed tests with sites like GTmetrix, they all complain about redirects. For example:
Remove the following redirect chain if possible:
h-t-t-p://mysite.com/directory/images/shorter.png
h-t-t-p://www.mysite.com/directory/images/shorter.png
I suppose this is due to these lines in my main .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]
So it seems I have a catch22 here. The search engines don't want my site to appear to have duplicate content, and the redirect is a factor in slowing things down.
What is the best practice here? It seems a common situation, but I can't find any solutions by Googling. Should I redefine document_root, so the redirect at the registrar level, or what?
Thanks for your thoughts on this.
Avoid unnecessary static content redirects by updating the src and href attributes in your HTML to use the full domain name including the www.
Or, use absolute paths without the domain name at all.
Or, add a rule to exclude some types of files:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^mysite.com [NC]
RewriteCond %{REQUEST_URI} !\.(js|css|gif|jpeg|png|jpg|pdf|ico)$ [NC]
RewriteRule ^(.*)$ http://www.mysite.com/$1 [L,R=301]

.htaccess redirect/rewrite from one url to another, and subpages to other pages

I'm having some difficulty trying to get a correct rewrite/redirect that will do what I want and I'm wondering if it's even possible.
I have this rewrite setup:
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^olddomain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.olddomain.com$
RewriteRule (.*) http://www.newdomain.com/ [R=301,L]
This works great to take all requests from olddomain.com/whatever and rewrite them to my new main url. However, I also would like to be able to take 10 or 15 of the most popular "old" urls and rewrite/redirect them to the new location on the new url. My directory structure changed with the changeover to new url, so all the new urls are different and things are located in different places. Is it possible to have all traffic from the old domain redirected to the new domain AND specify a few old urls to redirect to new urls on the new domain?
I hope this is clear enough and I'm hoping someone can assist me. Thanks for your time!
Tom
Here's a solution:
.htaccess for old domain
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
In the above, you could place redirect rules to catch certain pages and forward them to their new location, like this:
Redirect 301 /2012/05/old-post http://www.newdomain.com/old-post
Be sure to place the redirect directives above the rewrite rule that catches everything else.
.htaccess for new domain
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.[a-z-]+\.[a-z]{2,6} [NC]
RewriteCond %{HTTP_HOST} ([a-z-]+\.[a-z]{2,6})$ [NC]
RewriteRule ^/(.*)$ http://%1/$1 [R=301,L]
Found this solution on: https://forums.digitalpoint.com/threads/htaccess-redirect-all-traffic-to-another-site.867280/
I've done similar things, so hopefully it helps.

Redirecting all subdomains to one page

I'm trying to redirect all subdomains at (http://*.domain.com) to spesific html file (http://domain.com/index.html). But I also want to visible subdomain name won't change.
I've tried this one with .htaccess:
Options -MultiViews
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
#RewriteCond %{REQUEST_URI} !^/parent/
RewriteCond %{HTTP_HOST} ^([^\.]+)\.domain\.com$
RewriteRule (.*) /index.html [L]
But it doesn't work at all.
Can anyone help me?
Subdomains are a bit different than directories. You need to have a wildcard DNS record to catch all subdomains:
*.example.com. 3600 IN A host1.example.com.
Best thing to do would be to contact your hosting provider. Some of them have settings to achieve that.

Resources