【问题标题】:Why is .htaccess RewriteRule not working为什么.htaccess RewriteRule 不起作用
【发布时间】:2016-03-06 20:20:10
【问题描述】:

我有两个重写规则,第一个有效,第二个无效。谁能解释一下原因?

我希望任何带有“online-marketplace-development”文本的网址重定向到网址http://jeyjoo.com/blog/online-marketplace-development-what-we-learnt

mod_rewrite 已打开,所有其他规则都有效。我的 .htaccess 如下:

RewriteEngine On
##following rule works
RewriteRule ^blog/blog(.*)$ http://jeyjoo.com/blog$1 [L,R=301]
##the following rule doesn't seem to do anything
RewriteRule ^online-marketplace-development(.*)$ http://jeyjoo.com/blog/online-marketplace-development-what-we-learnt

根据要求,完整的 .htaccess 如下:

<IfModule mod_deflate.c>
    #The following line is enough for .js and .css
    AddOutputFilter DEFLATE js css

    #The following line also enables compression by file content type, for the following list of Content-Type:s
    AddOutputFilterByType DEFLATE text/html text/plain text/xml application/xml

    #The following lines are to avoid bugs with some browsers
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    BrowserMatch ^Mozilla/4\.0[678] no-gzip
    BrowserMatch \bMSIE !no-gzip !gzip-only-text/html 
</IfModule>

<IfModule mod_headers.c>
    <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
    </FilesMatch>
    # 1 YEAR
    <FilesMatch "\.(ico|jpg|jpeg|png|gif|swf)$">
    Header set Cache-Control "max-age=29030400, public"
    </FilesMatch>
    # 1 WEEK is 604800
    <FilesMatch ".(xml|txt|css|js)$">
    Header set Cache-Control "max-age=904800, proxy-revalidate"
    </FilesMatch>
    # 1 MIN
    #<FilesMatch ".(html|htm|php)$">
    #Header set Cache-Control "max-age=60, private, proxy-revalidate"
    #</FilesMatch>
</IfModule>
##
# READ THIS COMPLETELY IF YOU CHOOSE TO USE THIS FILE!
#
# The line just below this section: 'Options +FollowSymLinks' may cause problems
# with some server configurations.  It is required for use of mod_rewrite, but may already
# be set by your server administrator in a way that disallows changing it in
# your .htaccess file.  If using it causes your server to error out, comment it out (add # to
# beginning of line), reload your site in your browser and test your sef url's.  If they work,
# it has been set by your server administrator and you do not need it set here.
##

## No directory listings
IndexIgnore *

## Can be commented out if causes errors, see notes above.
Options +FollowSymlinks
Options -Indexes

## Mod_rewrite in use.

RewriteEngine On

## Begin - Rewrite rules to block out some common exploits.
# If you experience problems on your site block out the operations listed below
# This attempts to block the most common type of exploit `attempts` to Joomla!
#
# Block out any script trying to base64_encode data within the URL.
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
# Block out any script that includes a <script> tag in URL.
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
# Block out any script trying to set a PHP GLOBALS variable via URL.
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
# Block out any script trying to modify a _REQUEST variable via URL.
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
# Return 403 Forbidden header and show the content of the root homepage
RewriteRule .* index.php [F]
#
## End - Rewrite rules to block out some common exploits.

## Begin - Custom redirects

RewriteRule ^blog/blog(.*)$ http://jeyjoo.com/blog$1 [L,R=301]
RewriteRule ^online-marketplace-development(.*)$ http://jeyjoo.com/blog/online-marketplace-development-what-we-learnt 
RewriteCond %{HTTP_HOST} ^www\.jeyjoo\.com [NC]
RewriteRule ^(.*)$ http://jeyjoo.com/$1 [R=301,NC,L]

## End - Custom redirects

##
# Uncomment following line if your webserver's URL
# is not directly related to physical file paths.
# Update Your Joomla! Directory (just / for root).
##

# RewriteBase /

## Begin - Joomla! core SEF Section.
#
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#
# If the requested path and file is not /index.php and the request
# has not already been internally rewritten to the index.php script
RewriteCond %{REQUEST_URI} !^/index\.php
# and the requested path and file doesn't directly match a physical file
RewriteCond %{REQUEST_FILENAME} !-f
# and the requested path and file doesn't directly match a physical folder
RewriteCond %{REQUEST_FILENAME} !-d
# internally rewrite the request to the index.php script
RewriteRule .* index.php [L]
#
## End - Joomla! core SEF Section.

谢谢

【问题讨论】:

  • 为什么不:RewriteRule ^blog/online-marketplace-development.*$ http://jeyjoo.com/blog/online-marketplace-development-what-we-learnt
  • @anubhava 我刚试过这个 - 但仍然没有改变。
  • 不,只有顶层文件
  • Ji @anubhava ,我按照建议移到 RewriteEngine 的开头-但仍然没有运气,我还删除了缓存并在隐身模式下打开浏览器以避免页面缓存-仍然没有重定向

标签: apache .htaccess mod-rewrite url-rewriting rewrite


【解决方案1】:

[L] 标志导致 mod_rewrite 停止处理规则集。在大多数情况下,这意味着如果规则匹配,则不会处理进一步的规则。 see Apache docs on L flag

规则 1 已应用于您的第二条规则,因此请从这个角度重新考虑您的第二条规则。

【讨论】:

  • 这不是 [L] 标志的工作方式。 L 将告诉重写引擎在规则匹配后停止。在这种情况下,第一条规则尚未匹配,因此不会触发 L 标志。此外,一旦 URL 被重写,重写规则将针对新重写的 URL 重新运行。
  • 1:我评论的第一行是 Apache docs 和的准确引用
  • 2:有问题的 RewriteRule 中的模式说:^(= 如果以下内容位于行首)并且您可能希望它说:只需匹配 'online-marketplace- 的每个实例发展(等)'。所以我认为 online-marketplace-development(.*) (没有 ^ 和 $)可以随心所欲地工作。
  • 这也不起作用。就像我说的,上面相同格式(博客/博客)的规则确实有效。
  • 再一次,我认为您想看到的内容已经在第一条规则中得到处理,并且该规则上的 L 标志已停止进一步处理。
猜你喜欢
  • 2012-02-17
  • 2012-12-10
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-06-22
相关资源
最近更新 更多