大网数据广告活动
服务器运维问答

Rewrite not working in .htaccess file

· 52CE 我爱测

Update 6/26/18: I appreciate that this may be a duplicate of Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask however, I did mention in …

问题原文

原帖链接 · Michael Purvis · CC BY-SA 4.0

Update 6/26/18: I appreciate that this may be a duplicate of Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask however, I did mention in my original post that I had been to that post and found the tester site from that post. I did read that post, and while I am still a novice to rewrite rules, I don't think this is duplicate; however I could be wrong.

I am trying to set up a new rewrite rule within my .htaccess file (below)

    SetEnv HTTPS on

<IfModule mod_rewrite.c>
    RewriteEngine On
Options -Indexes
    # Send would-be 404 requests to Craft
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
    RewriteRule (.+) index.php?p=$1 [QSA]
Options - Autodiscover
   RewriteEngine On
   RewriteCond %{REQUEST_FILENAME} -s [OR]
   RewriteCond %{REQUEST_FILENAME} -l [OR]
   RewriteCond %{REQUEST_FILENAME} -d
   RewriteRule ^.*$ - [NC,L]
   RewriteRule ^.*$ autodiscover.php [NC,L]
</IfModule>

I found this online tester within this post: Redirect, Change URLs or Redirect HTTP to HTTPS in Apache - Everything You Ever Wanted to Know About Mod_Rewrite Rules but Were Afraid to Ask

Link to the tester: .htaccess tester

When I ran my configuration, I got the following output debugging info for .htaccess

My Question:

  1. How do I make sure that the index.php is not getting routed wrongly based on the output of the 'debugging info for .htaccess'?
    In the output of the 'debugging info for .htaccess', the index.php is getting appended to the autodiscover.xml -- which is NOT what I need to happen.
  2. What is wrong with my configuration that causes it to fail regarding the Autodiscover portion? The 'debugging info for .htaccess' states that the condition was met only because of the 'OR' in my configuration.

Thank you to anyone that see this and offer's any kind of assistance or explanation!! :-)

已采纳解答

原帖链接 · Esa Jokinen · CC BY-SA 4.0

The last rule doesn't have any conditions, so it is used always.

RewriteRule ^.*$ autodiscover.php [NC,L]

The RewriteCond directive defines a rule condition. One or more RewriteCond can precede a RewriteRule directive. The following rule is then only used if both the current state of the URI matches its pattern, and if these conditions are met.

Therefore, all your conditions are related to the previous rule, instead:

RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]

Also:

  • You only need RewriteEngine On once.

  • Options- Autodiscover would have a syntax error IF there was such a server feature.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-06-21)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。