I have 3 domains, for the same web app, since I don't want to have duplicated content I want to redirect the other two domains to the primary domain.
If a user types client_xyz.domain2.com it should be redirected to client_xyz.primarydomain.com.
If a user types client_xyz.domain2.com/folder/file/etc it should be redirected to client_xyz.primarydomain.com/folder/file/etc.
If a user types domain2.com/test/page it should be redirected to primarydomain.com/test/page
I thought this is the best solution to avoid Google penalty for duplicated content.
If you think there's a better solution to deal with this (eg. DNS), let me know.
Put this code in your .htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteCond %{HTTP_HOST} ^(client_xyz\.)?domain2\.com$ [NC]
RewriteRule ^ http://%1primarydomain.com%{REQUEST_URI} [NE,R=301,L]
You can user regex in the .htaccess file, so set up a 301 (moved permanently) redirect.
RedirectMatch 301 (^.*?\.|^)domain2.com(.*) $1.primarydomain.com$2
Untested, but should be fine.
[edit]The below is fairly useless since your comment, but I'll leave it here anyway.
Alternatively, you could set up server alias's in your Apache config file. You probably have something like this:
ServerName primarydomain.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
AllowOverride All
</Directory>
Change it to:
ServerName primarydomain.com
ServerAlias domain2.com
DocumentRoot /var/www/html/mysite/
<Directory "/var/www/html/mysite/">
AllowOverride All
</Directory>
If you can use mod_rewrite, this should work:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} (^.*?\.|^)?domain2\.com
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([a-z0-9-]+)/? http://$1.primarydomain.com [R=301,NC,L]
If not, well, I'm probably going to have to leave it up to someone smarter than me ;-)
Related
My .htaccess won't redirect a URL correctly for some strange reason, whenever I attempt direct traffic from http://example.com/b70a0 to then into http://example.com/serve/?short=b70a0 I get a 404 page as if the page was not found, when of course there was never going to be a page there in the first place, I wanted to redirect the traffic from that page.
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ serve.php?short=$1 [QSA,L]
I found promptly the answer to my issue, turns out I needed to have "AllowOverride" to be allowed inside of my actual Apache settings, as that can be found by heading into the
/etc/apache2/sites-available/
folder, the following is what I found to be useful when making this resource.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
allow from all
</Directory>
I am trying to replace a word in URL and redirect the user using .htaccess
I have this link,
/anyword/akram/anyotherword
and I would like to redirect user to
/anyword/tanger/anyotherword
Here is the code I have tried
RewriteRule ^(.*)akram(.*)$ $1tanger$2 [R=301,L]
Edit
Virtualhost:
<Directory /var/www/html/mywebsite.com/web/>
Options Indexes FollowSymLinks MultiViews
AllowOverride None
Order allow,deny
allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
RewriteRule ^(.+)/akram/(.+)$ http://%{HTTP_HOST}/$1/tanger/$2 [R=301,L]
</IfModule>
</Directory>
</VirtualHost>
You can try this rule.
RewriteRule ^(.+)/akram/(.+)$ http://%{HTTP_HOST}/$1/tanger/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
you should create a new file and save it as .htaccess
After that . Edit the contents of the file. For 301 (Permanent) Redirect: Point an entire site to a different URL on a permanent basis.
# This allows you to redirect your entire website to any other domain
Redirect 301 / http://***/anyword/tanger/anyotherword
I'm answering quite late.
but I think somebody can get it helpful..
RewriteEngine on
RewriteRule /akram/ /tanger/
checkout
Open link, and click on "test"
I need to redirect subfolder to subdomain using htaccess
For example:
redirect www.example.com/test (also http://, https://, http://www, https://www)
to
https://user.example.com
how do I do it?
also, how it should be if I need to redirect it to https://user.example.com/test
You can put this code in your htaccess (which has to be in root folder)
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?example\.com$ [NC]
RewriteRule ^test/?$ https://user.example.com/test [R=301,L]
Don't forget to enable mod_rewrite if it's not yet done
Look at original answer at
Can you do a different for blog.example.com?
If you can, just put your Redirect config there:
<VirtualHost *:80>
ServerName blog.example.com
Redirect / http://example.com/blog/
</VirtualHost>
If for some reason you still need to piggy-back on the other vhost, or need to handle all subdomains and not just "blog", then use mod_rewrite:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([^\.]+)\.example\.com$
RewriteRule ^/(.*)$ http://example.com/%1/$1 [R=301,L]
I'm trying to enable the rewrite function in drupal 7 without using the .htaccess file since it should only be used if you do not have root access to the server (accordingly to apache's website)
Without any further adieu, here is my configuration
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DirectoryIndex index.php
DocumentRoot /var/www/example.com/public
ErrorDocument 404 /index.php
LogLevel warn
ErrorLog /var/www/example.com/log/error.log
CustomLog /var/www/example.com/log/access.log combined
<Directory "/var/www/example.com/public">
Options -Indexes +FollowSymLinks +ExecCGI
AllowOverride None
Order allow,deny
Allow from all
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ /index.php?q=$1 [L,QSA]
</Directory>
</VirtualHost>
The rewrite configuration worked when I had it in the .htaccess file but not when I put it inside the vhost file.
Can you guys see anything wrong here?
I think your rewrite rule should not contain the '/' prefix now that it's not a .htaccess anymore. Soo try with:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Removing .htaccess reference is a good idea. You should put the AllowOverride None on a <Directory /> instruction so that this start from the root of the filesystem. If think you do not need the +ExecCGI also. If you want to go deeper in apache config for Drupal without .htaccess check also this detailled resource.
Ok I am serving two domains off of one box. The domains are:
www.old.org and www.new.com. As it is now, all of the files and dirs are the same on both domains.
I want to make it so that IF someone goes to www.old.org/folder_a/file_b.php they are 301'ed to www.new.com/folder_a/file_b.php.
I've already tried in the htaccess:
RedirectMatch 301 ^/ http://www.new.com/
But that give a 301 loop because the 301's condition still applies after the 301 is enacted. I think I want to do something that uses rewritecond %{HTTP_HOST} ^.*old.org$ so that only url's at old.org or www.old.org will be affected, but I'm not sure how to do this.
If you have access to the apache vhost configs use those instead of the .htaccess:
<VirtualHost *:80>
ServerName www.old.org
Redirect permanent / http://www.new.com/
</VirtualHost>
If you really must use an .htaccess the following will do:
RewriteEngine On
RewriteCond %{SERVER_NAME} =www.old.org [NC]
RewriteRule (.*) http://www.new.com/$1 [R=301,L]
Some thing like this should do:
RewriteCond %{http_host} ^www\.old\.org$ [NC]
RewriteRule ^/(.*) http://www.new.com/$1 [R=301]