简体   繁体   中英

Apache: Using VirtualHost redirect to URL with RewriteCond applied?

So I have a RewriteCond that turns:

domain.com/view.php?a=1

into:

domain.com/artist/name

I have a shorter domain: dmn.com that I would like to provide shortlinks for artists:

dmn.com/name

How can I get dmn.com/name to redirect to: domain.com/artist/name while keeping the shortlink in tact?

.htaccess:

#rewrite profile requests
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule artist/(.*)$ view.php?a=$1 [QSA,NC,L]

httpd.conf:

<VirtualHost *:80>
    DocumentRoot /var/www/html/ // <-- ??? not sure what to put here
    ServerName dmn.com
    ServerAlias www.dmn.com
</VirtualHost>

Since the directory "/artist" doesn't actually exist (since it's being rewritten), I can't seem to put /var/www/html/artist in the DocumentRoot of the VirtualHost config without Apache spitting back:

Warning: DocumentRoot [/var/www/html/artist] does not exist

I think what you want is not an actual redirection, but another rewrite.

You can't do it with a separate VirtualHost . Well, you can if you point it to the same DocumentRoot and duplicate your rules or load them from a common file, either via .htaccess or a Include directive.

You can also add the short domain name to your main domain ServerAlias

<VirtualHost *:80>
    # This should point to your current config
    DocumentRoot /var/www/artists_profiles/
    ServerName example.com
    ServerAlias www.example.com ex.com www.ex.com
</VirtualHost>

Then you add the new rules to the top of the file:

RewriteBase /
RewriteCond %{HTTP_HOST} ex.com
RewriteRule ^(^.*) artist/$1

Then your existing rules should pick up. If you want to avoid the shortened domain from responding to the longer urls you could add a similar RewriteCond to the existing rules:

RewriteCond %{HTTP_HOST} !ex.com

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