Many posters have problems debugging their RewriteRule and RewriteCond statements within their .htaccess files. Most of these are using a shared hosting service and therefore don't have access to the root server configuration. They cannot avoid using .htaccess files for rewriting and cannot enable a RewriteLogLevel" as many respondents suggest. Also there are many .htaccess-specific pitfalls and constraints are aren't covered well. Setting up a local test LAMP stack involves too much of a learning curve for most.

所以我的问题是我们建议他们如何自己调试他们的规则。以下是我的一些建议。其他建议将不胜感激。

Understand that the mod_rewrite engine cycles through .htaccess files. The engine runs this loop: do execute server and vhost rewrites (in the Apache Virtual Host Config) find the lowest "Per Dir" .htaccess file on the file path with rewrites enabled if found(.htaccess) execute .htaccess rewrites (in the user's directory) while rewrite occurred So your rules will get executed repeatedly and if you change the URI path then it may end up executing other .htaccessfiles if they exist. So make sure that you terminate this loop, if necessary by adding extra RewriteCond to stop rules firing. Also delete any lower level .htaccess rewrite rulesets unless explicitly intent to use multi-level rulesets. Make sure that the syntax of each Regexp is correct by testing against a set of test patterns to make sure that is a valid syntax and does what you intend with a fully range of test URIs. See answer below for more details. Build up your rules incrementally in a test directory. You can make use of the "execute the deepest .htaccess file on the path feature" to set up a separate test directory (tree) and debug rulesets here without screwing up your main rules and stopping your site working. You have to add them one at a time because this is the only way to localise failures to individual rules. Use a dummy script stub to dump out server and environment variables. (See Listing 2)If your app uses, say, blog/index.php then you can copy this into test/blog/index.php and use it to test out your blog rules in the test subdirectory. You can also use environment variables to make sure that the rewrite engine in interpreting substitution strings correctly, e.g. RewriteRule ^(.*) - [E=TEST0:%{DOCUMENT_ROOT}/blog/html_cache/$1.html] and look for these REDIRECT_* variables in the phpinfo dump. BTW, I used this one and discovered on my site that I had to use %{ENV:DOCUMENT_ROOT_REAL} instead. In the case of redirector looping REDIRECT_REDIRECT_* variables list the previous pass. Etc.. Make sure that you don't get bitten by your browser caching incorrect 301 redirects. See answer below. My thanks to Ulrich Palha for this. The rewrite engine seems sensitive to cascaded rules within an .htaccess context, (that is where a RewriteRule results in a substitution and this falls though to further rules), as I found bugs with internal sub-requests (1), and incorrect PATH_INFO processing which can often be prevents by use of the [NS], [L] and [PT] flags.

还有什么评论或建议吗?

清单1——phpinfo

<?php phpinfo(INFO_ENVIRONMENT|INFO_VARIABLES);

我想禁用目录浏览/画廊文件夹和所有子目录

/画廊索引/409 *父目录 * i1269372986681.jpg * i1269372986682.jpg * i1269372988680.jpg

我在/etc/apache2/sites下有cmsplus.dev -可用以下代码,

<VirtualHost *:80>
    ServerAdmin master@server.com
    ServerName www.cmsplus.dev
    ServerAlias cmsplus.dev

    DocumentRoot /var/www/cmsplus.dev/public

    LogLevel warn
    ErrorLog /var/www/cmsplus.dev/log/error.log
    CustomLog /var/www/cmsplus.dev/log/access.log combined
</VirtualHost>

现在当我使用sudo /usr/sbin/a2ensite cmsplus.dev时,我得到了错误,

ERROR: Site cmsplus.dev does not exist!

我的web服务器Apache/2.4.6 (Ubuntu)

如何解决这个问题?

我想设置我的本地开发机器,以便任何对*的请求。Local被重定向到localhost。这个想法是,当我开发多个站点时,我只需向Apache添加名为site1的vhosts。地方,site2。让它们都解析到localhost,而Apache则相应地服务于不同的站点。

我用的是Windows XP。

我试着加上

127.0.0.1       *.local

到我的c:\windows\system32\drivers\etc\hosts文件,也试过:

127.0.0.1       .local

这两种方法似乎都不管用。

我知道我可以在不同的端口号上设置它们,但这是一种痛苦,因为很难记住哪个端口是哪个端口。

我不想设置本地DNS服务器,有什么建议吗?

如何查看Debian机器上安装了Apache的哪个版本?

有这样做的命令吗?

是否可以配置xampp来提供htdocs目录之外的文件?

例如,假设我有一个文件的位置如下:

C: \ \ transitCalculator \树干\ TransitCalculator.php项目

我的xampp文件通常从:

C:\xampp\htdocs\

(因为这是默认配置)是否有一些方法让Apache识别和服务我的TransitCalculator.php文件,而不移动到htdocs下?最好我想Apache提供/有权访问项目目录的全部内容,我不想移动htdocs下的项目目录。

edit:编辑后将Apache添加到问题标题中,使Q/A更“可搜索”

我有以下htaccess代码:

<IfModule mod_rewrite.c>

RewriteEngine On
RewriteCond !{HTTPS} off
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

</IfModule>

我希望我的网站被重定向到https://www。使用HTTPS,并强制执行www。子域名, 但当我访问http://www时。(没有HTTPS),它不会重定向到https://www与HTTPS。

我正在研究如何用Python开发一个不错的web应用程序。因为我不想让一些高阶结构妨碍我,所以我选择了轻量级的Flask框架。时间会证明这是否是正确的选择。

So, now I've set up an Apache server with mod_wsgi, and my test site is running fine. However, I'd like to speed up the development routine by making the site automatically reload upon any changes in py or template files I make. I see that any changes in site's .wsgi file causes reloading (even without WSGIScriptReloading On in the apache config file), but I still have to prod it manually (ie, insert extra linebreak, save). Is there some way how to cause reload when I edit some of the app's py files? Or, I am expected to use IDE that refreshes the .wsgi file for me?

当我试图在本地浏览php文件时,我有这个错误

[Fri Apr 13 19:16:40 2012] [alert] [client 127.0.0.1] C:/AppServ/www/hr-website/.htaccess: Invalid command 'RewriteEngine', perhaps misspelled or defined by a module not included in the server configuration, referer: http://127.0.0.1/

有什么问题吗?

我想使用Node,因为它是快速的,使用与我在客户端使用的相同的语言,并且根据定义它是非阻塞的。但是我雇来写文件处理程序(保存、编辑、重命名、下载、上传文件等)的那个人,他想用apache。所以,我必须:

说服他使用Node(他在这方面几乎没有让步) 弄清楚如何上传,下载,重命名,保存等文件在节点或 我必须在同一台服务器上安装apache和node。

哪种情况是最有利的,我如何实现它?