SnapOverflow

SnapOverflow Logo SnapOverflow Logo

SnapOverflow Navigation

  • Home
  • Server
  • Ubuntu

Mobile menu

Close
  • Home
  • System Administrators
    • Hot Questions
    • New Questions
    • Tags
  • Ubuntu
    • Hot Questions
    • New Questions
    • Tags
  • Help
Home / server / Questions / 105806
Accepted
Deepak Mittal
Deepak Mittal
Asked: 2010-01-25 04:34:04 +0800 CST2010-01-25 04:34:04 +0800 CST 2010-01-25 04:34:04 +0800 CST

Apache Rewrite: How to write a rule based on domain name (instead of HTTP_HOST)?

  • 772

I have written a RewriteRule which works on the basis of HTTP_HOST (www.domainname.com), but I want it to work on the basis of the domain name part only (domainname.com).

To clarify further

The folder structure is /var/www/domainName.com.

When I write this rule in apache conf file

RewriteRule ^/js/(.*) /%{HTTP_HOST}/js/$1 [L]

and access the site using www.mydomain.com -- it tries to find the folder /var/www/www.domainName.com, which does not exist.

So, I need to convert the above-mentioned rule to remove the "www" from the HTTP_HOST.

apache-2.2 mod-rewrite
  • 4 4 Answers
  • 8119 Views

4 Answers

  • Voted
  1. Best Answer
    Jeff Snider
    2010-01-25T09:07:53+08:002010-01-25T09:07:53+08:00

    I would suggest that you redirect requests rather than make the same site available in two places. Either create a new VirtualHost to redirect traffic:

    <VirtualHost *:80>
        ServerName www.domainName.com
        Redirect / http://domainName.com/
    </VirtualHost>
    

    Or add a set of rules above your others that will detect the presence of the www. prefix and redirect the user:

    RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
    RewriteRule /(.*) http://%1/$1 [R,L]
    

    If you really want your site on both domain names without any redirection, you can use a RewriteCond to pull out just the domain name, and it will be available to the RewriteRule as %1.

    RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*) [NC]
    RewriteRule ^/js/(.*) /%1/js/$1 [L]
    
    • 3
  2. rytis
    2010-01-25T04:42:55+08:002010-01-25T04:42:55+08:00

    I doubt if you can do that, wouldn't it be easier just to create links to appropriate directories?

    ln -s yourdomain.com www.yourdomain.com
    
    • 1
  3. touchstone
    2010-01-25T05:41:38+08:002010-01-25T05:41:38+08:00

    Have you tried using %{DOCUMENT_ROOT}?

    Also possible, if you have set Servername in your vhost to the same name you use in your directory structure, eg:

    <VirtualHost *:80>
      DocumentRoot /var/www/domainame.com
      ServerName domainame.com
      ServerAlias www.domainame.com
    </VirtualHost>
    

    Then you can use %{SERVER_NAME} in your rewrite-rule.

    • 1
  4. Christian
    2010-01-25T04:52:51+08:002010-01-25T04:52:51+08:00

    as i understand you question, just set your DocumentRoot to the right directory:

    DocumentRoot /var/www/domainName.com
    
    • 0

Sidebar

Stats

  • Questions 681965
  • Answers 980273
  • Best Answers 280204
  • Users 287326
  • Popular
  • Answers
  • Marko Smith

    Ping a Specific Port

    • 18 Answers
  • Marko Smith

    How do I tell Git for Windows where to find my private RSA key?

    • 30 Answers
  • Marko Smith

    How do you restart php-fpm?

    • 18 Answers
  • Marko Smith

    What's the default superuser username/password for postgres after a new install?

    • 5 Answers
  • Marko Smith

    What port does SFTP use?

    • 6 Answers
  • Marko Smith

    Resolve host name from IP address

    • 8 Answers
  • Marko Smith

    How can I sort du -h output by size

    • 30 Answers
  • Marko Smith

    Command line to list users in a Windows Active Directory group?

    • 9 Answers
  • Marko Smith

    What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats?

    • 3 Answers
  • Marko Smith

    How to determine if a bash variable is empty?

    • 15 Answers
  • Martin Hope
    Davie Ping a Specific Port 2009-10-09 01:57:50 +0800 CST
  • Martin Hope
    binaryorganic How do I tell Git for Windows where to find my private RSA key? 2010-10-26 08:45:39 +0800 CST
  • Martin Hope
    tobym What exactly do the colors in htop status bars mean? 2010-09-14 12:22:43 +0800 CST
  • Martin Hope
    MikeN In Nginx, how can I rewrite all http requests to https while maintaining sub-domain? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner How can I sort du -h output by size 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 What is the difference between double and single square brackets in bash? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt How does IPv4 Subnetting Work? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich What is a Pem file and how does it differ from other OpenSSL Generated Key File Formats? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent How to determine if a bash variable is empty? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus How do you find what process is holding a file open in Windows? 2009-05-01 16:47:16 +0800 CST

Related Questions

Trending Tags

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • Home
  • Questions
    • Hot Questions
    • New Questions
  • Tags
  • Help

Footer

SnapOverflow

About Us

  • About Us
  • Contact Us

Legal Stuff

  • Privacy Policy

Help

© 2022 SOF-TR. All Rights Reserve