代码之家  ›  专栏  ›  技术社区  ›  Tim Santeford

如何编写一个.htaccess,根据请求url进行不同的重写?

  •  0
  • Tim Santeford  · 技术社区  · 17 年前

    我希望有一个.htaccess文件,可以在本地主机开发环境和托管生产站点上正确重写。现在我必须为我工作的每个站点保留两份单独的文件副本。我希望能够同步这两个站点,而不必吹走它们的任何一个.htaccess文件。

    <IfModule mod_rewrite.c>
        RewriteEngine On
        RewriteBase /
    
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d
        RewriteCond $1 !^(index\.php|images|robots\.txt|css)
    
    ## IF the url looks like its from localhost then use this rule
      ## The incoming url might look like: http://localhost/mysite/about
        RewriteRule ^(.*)$ /mysite/index.php/$1 [L]
    ## Else do this rewrite
      ## The incoming url might look like: http://www.mysite.com/about
        RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
    </IfModule>
    
    <IfModule !mod_rewrite.c>
        ErrorDocument 404 /index.php
    </IfModule> 
    

    以下是我的服务器配置:

    发展 :windows上的xampp

    生产 :Dreamhost

    3 回复  |  直到 17 年前
        1
  •  1
  •   Mark Bell    17 年前

    我对此有点生疏,但我认为:

    RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
    
    #Do your live stuff here
    
    RewriteCond %{HTTP_HOST} ^localhost$ [NC]
    
    #Do your local stuff here
    
        2
  •  0
  •   Laurence Gonsalves    17 年前

    使用RewriteCond检查%{HTTP_HOST}。如:

    RewriteCond %{REMOTE_HOST}  ^localhost$
    
        3
  •  0
  •   Gumbo    17 年前

    virtual host 用于与生产服务器具有相同设置的开发环境。

    再加一个 <VirtualHost> 容器到您的 httpd.conf httpd-vhosts.conf

    <VirtualHost *:80>
        ServerName example.com.local
        DocumentRoot /absolute/filesystem/path/to/your/localhost/mysite
    </VirtualHost>