.htaccess:有些重写有效,而另一些则无效



下面是我的.htaccess文件的一个片段:

  Options +FollowSymlinks
  RewriteEngine on
  RewriteRule spanish-vocabulary-bible http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  RewriteRule books/svb http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  #Rewrite rule for spanish vocabulary bible
  RewriteRule svb http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  #Rewrite rule for emails
  RewriteRule emails http://peppyburro.com:2082/cpsess5562708231/frontend/paper_lantern/mail/pops.html [NC,L]

在这些规则中,除第二条规则外,所有规则都运行良好。我正在疯狂地想这里可能出了什么问题。我唯一能猜测的是,任何涉及子文件夹结构作为别名的规则(例如books/svb)都不起作用。如果你注意到那些有效的,它们都没有"/"-svb、电子邮件、西班牙语词汇圣经。这意味着什么,还是只是巧合?我已经使用其他几个名称和组合进行了测试,结果是一致的。

更新:基于下面的第一个答案,我将第二次重写改为:

RewriteRule books/svb/?$ http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]

它仍然不起作用。

更新2:我刚刚在第二条规则中将books/svb更改为tome/svb,它起作用了。但这有两个问题:

  1. 我需要它是书,而不是大部头或其他任何东西
  2. 重定向是透明的,即在地址栏中显示重定向后的URL;我希望重定向的URL(带有POST数据的URL)被隐藏

根据下面anubhava的请求,这里是我整个.htaccess文件的转储。恐怕很长:

AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
  Options +FollowSymlinks
  RewriteEngine on
  Header set Access-Control-Allow-Origin "*"
  <IfModule mod_proxy_http.c>
    RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator|visionutils|Facebot|Facebook [NC,OR]
    RewriteCond %{QUERY_STRING} _escaped_fragment_
    # Rewrite all dictionary words to a prerendered dictionary page
    RewriteRule ^dictionary/.* http://service.prerender.io/http://www.peppyburro.com/dictionary/ [P,L]
  </IfModule>
  <IfModule mod_proxy_http.c>
    RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator|visionutils|Facebot|Facebook [NC,OR]
    RewriteCond %{QUERY_STRING} _escaped_fragment_
    # Rewrite all dictionary words to a prerendered dictionary page
    RewriteRule ^conjugator/.* http://service.prerender.io/http://www.peppyburro.com/conjugator/ [P,L]
  </IfModule>
  <IfModule mod_proxy_http.c>
    RewriteCond %{HTTP_USER_AGENT} baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora link preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator|visionutils|Facebot|Facebook [NC,OR]
    RewriteCond %{QUERY_STRING} _escaped_fragment_
    # Only proxy the request to Prerender if it's a request for HTML
    RewriteRule ^(?!.*?(.js|.css|.xml|.less|.png|.jpg|.jpeg|.gif|.pdf|.doc|.txt|.ico|.rss|.zip|.mp3|.rar|.exe|.wmv|.doc|.avi|.ppt|.mpg|.mpeg|.tif|.wav|.mov|.psd|.ai|.xls|.mp4|.m4a|.swf|.dat|.dmg|.iso|.flv|.m4v|.torrent|.ttf|.woff))(.*) http://service.prerender.io/http://www.peppyburro.com/$2 [P,L]
  </IfModule>
  #<FilesMatch ".(pdf|PDF)">
  #  ForceType application/pdf
  #  Header set Content-Disposition attachment
  #</FilesMatch>
  RewriteCond %{HTTP_HOST} !^www. [NC]
  RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
  RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
  #RewriteRule ^/(.*)$ http://www.peppyburro.com/$1 [R=301,L]
  RewriteCond %{REQUEST_FILENAME} -s [OR]
  RewriteCond %{REQUEST_FILENAME} -l [OR]
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule ^.*$ - [NC,L]
  #Rewrite rule for blog feed
  RewriteRule feed blogs/feed/burrorss [NC,L]
  #Rewrite rule for blog admin (old)
  RewriteRule admin blogs/wp-admin [NC,L]
  #Rewrite rule for books
  RewriteRule spanish-vocabulary-bible http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  RewriteRule books/svb http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  #Rewrite rule for spanish vocabulary bible
  RewriteRule svb http://peppyburro.com/books?bb=spanish-vocabulary-bible [NC,L]
  #Rewrite rule for emails
  RewriteRule emails http://peppyburro.com:2082/cpsess5562708231/frontend/paper_lantern/mail/pops.html [NC,L]
  RewriteRule ^(.*) /index.php [NC,L]
</IfModule>
# ------------------------------------------------------------------------------
# | Proper MIME types for all files                                            |
# ------------------------------------------------------------------------------
<IfModule mod_mime.c>
  # Audio
    AddType audio/mp4                                   m4a f4a f4b
    AddType audio/ogg                                   oga ogg
  # JavaScript
    # Normalize to standard type (it's sniffed in IE anyways):
    # http://tools.ietf.org/html/rfc4329#section-7.2
    AddType application/javascript                      js jsonp
    AddType application/json                            json
  # Video
    AddType video/mp4                                   mp4 m4v f4v f4p
    AddType video/ogg                                   ogv
    AddType video/webm                                  webm
    AddType video/x-flv                                 flv
  # Web fonts
    AddType application/font-woff                       woff
    AddType application/vnd.ms-fontobject               eot
    # Browsers usually ignore the font MIME types and sniff the content,
    # however, Chrome shows a warning if other MIME types are used for the
    # following fonts.
    AddType application/x-font-ttf                      ttc ttf
    AddType font/opentype                               otf
    # Make SVGZ fonts work on iPad:
    # https://twitter.com/FontSquirrel/status/14855840545
    AddType     image/svg+xml                           svg svgz
    AddEncoding gzip                                    svgz
  # Other
    AddType application/octet-stream                    safariextz
    AddType application/x-chrome-extension              crx
    AddType application/x-opera-extension               oex
    AddType application/x-shockwave-flash               swf
    AddType application/x-web-app-manifest+json         webapp
    AddType application/x-xpinstall                     xpi
    AddType application/xml                             atom rdf rss xml
    AddType image/webp                                  webp
    AddType image/x-icon                                ico
    AddType text/cache-manifest                         appcache manifest
    AddType text/vtt                                    vtt
    AddType text/x-component                            htc
    AddType text/x-vcard                                vcf
</IfModule>

# ------------------------------------------------------------------------------
# | ETag removal                                                               |
# ------------------------------------------------------------------------------
# Since we're sending far-future expires headers (see below), ETags can
# be removed: http://developer.yahoo.com/performance/rules.html#etags.
# `FileETag None` is not enough for every server.
<IfModule mod_headers.c>
    Header unset ETag
  <filesMatch ".(ico|jpe?g|png|gif|swf)$">
    Header set Cache-Control "max-age=2592000, public"
  </filesMatch>
  <filesMatch ".(css)$">
    Header set Cache-Control "max-age=604800, public"
  </filesMatch>
  <filesMatch ".(js)$">
    Header set Cache-Control "max-age=216000, private"
  </filesMatch>
  <filesMatch ".(x?html?|php)$">
    Header set Cache-Control "max-age=600, private, must-revalidate"
  </filesMatch>
</IfModule>
FileETag None
# ------------------------------------------------------------------------------
# | Expires headers (for better cache control)                                 |
# ------------------------------------------------------------------------------
# The following expires headers are set pretty far in the future. If you don't
# control versioning with filename-based cache busting, consider lowering the
# cache time for resources like CSS and JS to something like 1 week.
<IfModule mod_expires.c>
    ExpiresActive on
    ExpiresDefault                                      "access plus 1 month"
  # CSS
    ExpiresByType text/css                              "access plus 1 year"
  # Data interchange
    ExpiresByType application/json                      "access plus 0 seconds"
    ExpiresByType application/xml                       "access plus 0 seconds"
    ExpiresByType text/xml                              "access plus 0 seconds"
  # Favicon (cannot be renamed!)
    ExpiresByType image/x-icon                          "access plus 1 week"
  # HTML components (HTCs)
    ExpiresByType text/x-component                      "access plus 1 month"
  # HTML
    ExpiresByType text/html                             "access plus 0 seconds"
  # JavaScript
    ExpiresByType application/javascript                "access plus 1 year"
  # Manifest files
    ExpiresByType application/x-web-app-manifest+json   "access plus 0 seconds"
    ExpiresByType text/cache-manifest                   "access plus 0 seconds"
  # Media
    ExpiresByType audio/ogg                             "access plus 1 month"
    ExpiresByType image/gif                             "access plus 1 month"
    ExpiresByType image/jpeg                            "access plus 1 month"
    ExpiresByType image/jpg                            "access plus 1 month"
    ExpiresByType image/png                             "access plus 1 month"
    ExpiresByType video/mp4                             "access plus 1 month"
    ExpiresByType video/ogg                             "access plus 1 month"
    ExpiresByType video/webm                            "access plus 1 month"
  # Web feeds
    ExpiresByType application/atom+xml                  "access plus 1 hour"
    ExpiresByType application/rss+xml                   "access plus 1 hour"
  # Web fonts
    ExpiresByType application/font-woff                 "access plus 1 month"
    ExpiresByType application/vnd.ms-fontobject         "access plus 1 month"
    ExpiresByType application/x-font-ttf                "access plus 1 month"
    ExpiresByType font/opentype                         "access plus 1 month"
    ExpiresByType image/svg+xml                         "access plus 1 month"
</IfModule>

<IfModule mod_deflate.c>
############################################
## enable apache served files compression
## http://developer.yahoo.com/performance/rules.html#gzip
    # Insert filter on all content
    SetOutputFilter DEFLATE
    # Insert filter on selected content types only
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
    # Netscape 4.x has some problems...
    BrowserMatch ^Mozilla/4 gzip-only-text/html
    # Netscape 4.06-4.08 have some more problems
    BrowserMatch ^Mozilla/4.0[678] no-gzip
    # MSIE masquerades as Netscape, but it is fine
    BrowserMatch bMSIE !no-gzip !gzip-only-text/html
    # Don't compress images
    SetEnvIfNoCase Request_URI .(?:gif|jpe?g|png)$ no-gzip dont-vary
    # Make sure proxies don't deliver the wrong content
    Header append Vary User-Agent env=!dont-vary
</IfModule>

# ------------------------------------------------------------------------------
# | GZip Compression (for faster page transfers)                               |
# ------------------------------------------------------------------------------
# GZip Compression
# compress text, html, javascript, css, xml:
#
<IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript
    AddOutputFilterByType DEFLATE application/rss+xml application/javascript application/x-javascript application/xml application/xhtml+xml
    AddOutputFilterByType DEFLATE application/x-font application/x-font-truetype application/x-font-ttf application/x-font-otf application/x-font-opentype application/vnd.ms-fontobject
    AddOutputFilterByType DEFLATE font/ttf font/otf font/opentype
    AddOutputFilterByType DEFLATE image/x-icon image/svg+xml
</IfModule>

这是因为您没有在正则表达式中使用锚点,而svb的第三条规则将覆盖books/svb

试试这些规则:

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^www. [NC]
RewriteCond %{HTTP_HOST} ^([a-z.]+)$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^spanish-vocabulary-bible/?$ books.php?bb=spanish-vocabulary-bible [NC,L,QSA]
RewriteRule (?:^|/)svb/?$ books.php?bb=spanish-vocabulary-bible [NC,L,QSA]
RewriteRule ^emails/?$ http://peppyburro.com:2082/cpsess5562708231/frontend/paper_lantern/mail/pops.html [NC,L,R]

请确保在测试之前清除浏览器缓存。

相关内容

最新更新