.htaccess mod_write禁止重写目录

时间:2017-04-15 11:19:55

标签: apache .htaccess mod-rewrite

我试图压制目录上的mod_write。目前,当我转到http://example.com/directory/file.php时,它会将我转到http://example.com/file.php。我希望它只是提供那里的file.php。我试过了

RewriteCond %{REQUEST_URI} ^/directory/.*$ [NC]
RewriteRule . - [L]

RewriteCond %{REQUEST_URI} !^directory/*$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]

似乎都不起作用。还有什么我可以尝试的吗?

这是完整档案......

#php_value safe_mode off
<IfModule pagespeed_module>
  ModPagespeed off
</IfModule>

# 1.To use URL Alias you need to be running apache with mod_rewrite enabled. 

# 2. In your opencart directory rename htaccess.txt to .htaccess.

# For any support issues please visit: http://www.opencart.com

Options +FollowSymlinks

# Prevent Directoy listing 
Options -Indexes

# SEO URL Settings
RewriteEngine On
# If your opencart installation does not run on the main web folder make sure you folder it does run in ie. / becomes /shop/ 

##Header add Access-Control-Allow-Origin "*" 
RewriteBase /

RewriteRule ^image/data/(.*)$ /image/catalog/$1 [R=301,NC,L]
############First Mod 12/16 Only One Address Per Site#########

RewriteRule ^sitemap.xml$ index.php?route=feed/google_sitemap [L]
RewriteRule ^googlebase.xml$ index.php?route=feed/google_base [L]
RewriteRule ^download/(.*) /index.php?route=error/not_found [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !.*\.(ico|gif|jpg|jpeg|png|js|css)
RewriteRule ^([^?]*) index.php?_route_=$1 [L,QSA]
RewriteCond %{REQUEST_URI} !pagespeed

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresDefault A0
</IfModule>
# Set up caching on media files for 5 weeks
<FilesMatch "\.(flv|ico|pdf|avi|mov|ppt|doc|mp3|wmv|wav)$">
ExpiresDefault A3024000
Header append Cache-Control "public"
</FilesMatch>

# Set up caching on media files for 5 weeks
<FilesMatch "\.(gif|jpg|jpeg|png|swf)$">
ExpiresDefault A3024000
Header append Cache-Control "public"
</FilesMatch>

# Set up 1 day caching on commonly updated files
<FilesMatch "\.(xml|txt|html|js|css)$">
ExpiresDefault A604800
Header append Cache-Control "proxy-revalidate"
</FilesMatch>

# Force no caching for dynamic files
<FilesMatch "\.(php|cgi|pl|htm)$">
ExpiresActive Off
Header set Cache-Control "private, no-cache, no-store, proxy-revalidate, no-transform"
Header set Pragma "no-cache"
</FilesMatch>

<IfModule mod_headers.c>
  <FilesMatch "\.(js|css|xml|gz)$">
    Header append Vary: Accept-Encoding
  </FilesMatch>
</IfModule>

 <IfModule mod_deflate.c>
    AddOutputFilterByType DEFLATE text/text text/html text/plain text/xml text/css application/x-javascript application/javascript image/x-icon
    <filesMatch "\.(css|js|x?html?|php|ico)$">
        SetOutputFilter DEFLATE
    </filesMatch> </ifModule>
<ifModule mod_headers.c>
    Header set Connection keep-alive
</ifModule>


<IfModule mod_filter.c>
    AddOutputFilterByType DEFLATE "application/atom+xml" \
                                  "application/javascript" \
                                  "application/json" \
                                  "application/ld+json" \
                                  "application/manifest+json" \
                                  "application/rdf+xml" \
                                  "application/rss+xml" \
                                  "application/schema+json" \
                                  "application/vnd.geo+json" \
                                  "application/vnd.ms-fontobject" \
                                  "application/x-font-ttf" \
                                  "application/x-javascript" \
                                  "application/x-web-app-manifest+json" \
                                  "application/xhtml+xml" \
                                  "application/xml" \
                                  "font/eot" \
                                  "font/opentype" \
                                  "image/bmp" \
                                  "image/svg+xml" \
                                  "image/vnd.microsoft.icon" \
                                  "image/x-icon" \
                                  "text/cache-manifest" \
                                  "text/css" \
                                  "text/html" \
                                  "text/javascript" \
                                  "text/plain" \
                                  "text/vcard" \
                                  "text/vnd.rim.location.xloc" \
                                  "text/vtt" \
                                  "text/x-component" \
                                  "text/x-cross-domain-policy" \
                                  "text/xml"

</IfModule>

2 个答案:

答案 0 :(得分:0)

请尝试以下操作,而不是REQUEST_FILENAME

RewriteCond %{SCRIPT_FILENAME} !-f [NC]
RewriteCond %{SCRIPT_FILENAME} !-d [NC]

我在其中一个网站上发现了同样的问题。

答案 1 :(得分:0)

  

我试图压制目录上的mod_write。

为此,您可以在该目录中创建一个.htaccess文件,并只启用重写引擎:

RewriteEngine On

默认情况下不会继承mod_rewrite指令,因此这会完全覆盖父.htaccess文件中的mod_rewrite指令。

RewriteCond %{REQUEST_URI} ^/directory/.*$ [NC]
RewriteRule . - [L]

这也应该有效,前提是它放在.htaccess文件的开头。顺便提一下,像^/directory/.*$这样的模式与简单的^/directory/相同。如果您使用的是Apache 2.4+,则可以使用END标志而不是L

RewriteCond %{REQUEST_URI} !pagespeed

这似乎是你的mod_rewrite规则结束时的迷路条件!?如果在其后面包含任何mod_rewrite指令(如在文件中的 where 中),这将导致问题。