简体   繁体   中英

Redirecting with ServerAlias with preserving the path

I have a site like www.myverylongdomainname.com, which is my main domain, and I also have a domain www.myshorturl.com which redirects to the longer one, set up like this:

<VirtualHost *:80>
    ServerAdmin blah@myverylongdomainname.com
    ServerName myverylongdomainname.com
    ServerAlias www.myverylongdomainname.com
    ServerAlias myshorturl.com
    ServerAlias www.myshorturl.com
    DocumentRoot /var/www/myverylongdomainname/public_html/dev
</VirtualHost>

We use the short URL for advertising and I'd like to be able to put this in a print ad: "www.myshorturl.com/keyword" and have the user end up on "myverylongdomainname.com/keyword". But I can't figure out how to preserve the path when the user is redirected to the full URL. I've done some googling but I'm not even sure what keywords to use.

Okay, I got help from this from a friend but here is the answer for anyone else looking:

You can't preserve path with ServerAlias (at least, I could not find a way). The solution is to set up myshorturl.com as its own site - set up its own VirtualHost pointing to its own directory. In that directory, add an .htaccess file with the code below - which redirects to the new URL and preserves the path.

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

Source for the code above: How to 301 redirect an entire domain while preserving the path

I've tested and confirmed this approach.

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