简体   繁体   English

在子域上设置Apache HTAccess重写规则

[英]Setting Apache HTAccess Rewrite Rules on a Subdomain

I am using a subdomain URL for my test server. 我正在为测试服务器使用子域URL。 Something like: 就像是:

http://dev.mysite.com http://dev.mysite.com

I have the following in my .htaccess file: 我的.htaccess文件中包含以下内容:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1
RewriteRule ^about/(.*)$ /about.php?request=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]

The above works well on the production (non-subdomain) set of URLs. 上面的方法在生产(非子域)URL集上效果很好。 But it doesn't quite make the cut on my dev box. 但这并不能完全减少我的开发人员。 For example, if I type in 例如,如果我输入

http://dev.mysite.com/about

I get a 404 error. 我收到404错误。 But the following with the appended .php extension works. 但是以下带有附加的.php扩展名的作品。

http://dev.mysite.com/about.php

I am guessing this has something to do with the first rewrite condition that deals with the www subdomain. 我猜想这与处理www子域的第一个重写条件有关。 How would I modify my .htaccess file to account for the dev subdomain? 如何修改.htaccess文件以解决dev子域?

It appears that your .htaccess is not enabled. 看来您的.htaccess没有启用。

Verify whether your .htaccess is enabled or not, by putting same garbage text on top of your .htaccess and see if it generates 500 (internal server) error or not? 验证是否 .htaccess启用与否,通过把相同的垃圾文字在你之上.htaccess ,看看它是否产生500(内部服务器)错误或不?

To enable .htaccess your httpd.conf file will need this line: 要启用 .htaccess您的httpd.conf文件将需要以下行:

AllowOverride All

You need at least AllowOverride All or AllowOverride FileInfo in order for the htaccess file to work in your server/vhost config. 您至少需要AllowOverride AllAllowOverride FileInfo才能使htaccess文件在服务器/ vhost配置中起作用。 FileInfo is for things like mod_rewrite. FileInfo用于mod_rewrite之类的东西。

Additionally, make sure you have mod_rewrite loaded. 此外,请确保已加载mod_rewrite。

You'll also want to change this condition: 您还需要更改此条件:

RewriteCond %{REQUEST_FILENAME}.php -f

to: 至:

RewriteCond %{DOCUMENT_ROOT}/$1.php -f

because mod_rewrite will recognize that about has a php file and make it so /about/something passes the -f test, resulting in /about/something.php . 因为mod_rewrite将识别about拥有一个php文件,并使其/about/something通过-f测试,从而生成/about/something.php

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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