简体   繁体   中英

.htaccess redirect doesn't work for me

I have ubuntu server with apache. Everythink works fine. Now I have 8 domains. One is linked with virtualhost. Let's call it maindomain.com. Other 7 domains are linked to another virtualhost, let's call it secondarydomain1.com to secondarydomain7.com.

I need these 7 domains to be redirected to main domain. Everything after / must be redirected to base domain. For example secondarydomain2.com/file.php will be redirected to maindomain.com

Can someone help me how to do it with .htaccess file?

I tried something like this, but it doesn't work.

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^secondarydomain\.com$ [NC]
RewriteRule ^(.*)$ http://maindomain.com [R=301,L]

The result is "You don't have permission to access /file.php on this server."

You need to enable rewrites in apache.

sudo a2enmod rewrite

Your rule isn't adding the request uri back to the domain part, which is why you aren't getting a redirection for the subparts as you expect.

Try this:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^secondarydomain\.com$ [NC]
RewriteRule ^(.*)$ http://maindomain.com%{REQUEST_URI} [R=301,L]

Tested on a test box:

curl http://192.168.163.161/test.html
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://maindomain.com/test.html">here</a>.</p>
<hr>
<address>Apache/2.2.15 (CentOS) Server at 192.168.163.161 Port 80</address>
</body></html>

I have some news. In fact, working redirects didn't really work. They were caused by Chrome cache. After cleaning cache, I get error "You don't have permission to access /info.php on this server." if .htaccess looks only like this:

RewriteEngine On

Btw. I can see mod_rewrite enabled in phpinfo.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM