.htaccess mod_rewrite 豁免

发布于 2024-12-09 05:56:21 字数 2832 浏览 0 评论 0原文

使用模板,我有这个 .htaccess mod_rewrite 代码,它使我的 URL 变得漂亮:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^posts.php$ posts.php [L]

# Rewrite www to no-www domain
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Rewrite multiple slashes with single slash after domain
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L,NE]

# Rewrite multiple slashes with single slash in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,NE,L]

# Remove slash from URL end
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,NE,L]

# Rewrite backslash with slash
## Works on FF with using AllowEncodedSlashes On in content/httpd.conf
RewriteCond %{REQUEST_URI} ^(.*)\\(.*)$
RewriteRule .* %1/%2 [R=301,NC,L]

# Rewrite query string, Ajax crawling
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^url=
RewriteCond %{QUERY_STRING} !api
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]

# Rewrite space with dash
RewriteCond %{REQUEST_URI} ^([^\ ]*)\ (.*)$
RewriteRule .* %1-%2 [R=301,L]

# Rewrite underscore with dash
RewriteCond %{REQUEST_URI} ^([^\_]*)\_(.*)$
RewriteRule .* %1-%2 [R=301,L]

# Remove dot
RewriteCond %{REQUEST_URI} ^([^\.]*)\.(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %1%2 [R=301,L]

# Remove comma
RewriteCond %{REQUEST_URI} ^([^\,]*)\,(.*)$
RewriteRule .* %1%2 [R=301,L]

# Remove dash before slash
RewriteCond %{REQUEST_URI} ^(.*)\-/(.*)$
RewriteRule ^(.*)\-/(.*)$  %1/%2 [R=301,L]

# Remove dash after slash
RewriteCond %{REQUEST_URI} ^(^\.)\/-(.*)$
RewriteRule ^(.*)\/-(.*)$  %1/%2 [R=301,L]

# Remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://%{HTTP_HOST}/%1$1 [R=301,L]

# Rewrite uppercase letter URL to lowercase with content/httpd.conf RewriteMap lc int:tolower
## Solution without httpd.conf RewriteMap in github.com/laukstein/ajax-seo/wiki/Lowercase-URL
RewriteCond %{REQUEST_URI} .+
RewriteRule ^[^A-Z]*[A-Z] ${lc:%0} [R=301,L]

# Block folder access that begins with a period (like .git, .svn)
RewriteRule "(^|/)\." - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/?([^\./]*)$ ?api&url=$1 [L,QSA]
RewriteRule ^([^.]*)$ ?url=$1 [L,QSA]

RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

我已经设法创建一个豁免以允许访问 posts.php 文件 - 但是我对如何允许我的页面访问存储在 /uploads/ 路径中的所有图像一无所知。

非常感谢,迈克

Using a template I have this .htaccess mod_rewrite code that makes my URL's pretty:

<IfModule mod_rewrite.c>
RewriteEngine On

RewriteRule ^posts.php$ posts.php [L]

# Rewrite www to no-www domain
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule .* http://%1%{REQUEST_URI} [R=301,L]

# Rewrite multiple slashes with single slash after domain
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L,NE]

# Rewrite multiple slashes with single slash in URL
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,NE,L]

# Remove slash from URL end
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^(.+)/$
RewriteRule ^.+/$ %1 [R=301,NE,L]

# Rewrite backslash with slash
## Works on FF with using AllowEncodedSlashes On in content/httpd.conf
RewriteCond %{REQUEST_URI} ^(.*)\\(.*)$
RewriteRule .* %1/%2 [R=301,NC,L]

# Rewrite query string, Ajax crawling
RewriteCond %{QUERY_STRING} ^_escaped_fragment_=(.*)$
RewriteRule .*$ %{REQUEST_URI}%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^.+$
RewriteCond %{QUERY_STRING} !^url=
RewriteCond %{QUERY_STRING} !api
RewriteRule .*$ %{REQUEST_URI}? [R=301,L]

# Rewrite space with dash
RewriteCond %{REQUEST_URI} ^([^\ ]*)\ (.*)$
RewriteRule .* %1-%2 [R=301,L]

# Rewrite underscore with dash
RewriteCond %{REQUEST_URI} ^([^\_]*)\_(.*)$
RewriteRule .* %1-%2 [R=301,L]

# Remove dot
RewriteCond %{REQUEST_URI} ^([^\.]*)\.(.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* %1%2 [R=301,L]

# Remove comma
RewriteCond %{REQUEST_URI} ^([^\,]*)\,(.*)$
RewriteRule .* %1%2 [R=301,L]

# Remove dash before slash
RewriteCond %{REQUEST_URI} ^(.*)\-/(.*)$
RewriteRule ^(.*)\-/(.*)$  %1/%2 [R=301,L]

# Remove dash after slash
RewriteCond %{REQUEST_URI} ^(^\.)\/-(.*)$
RewriteRule ^(.*)\/-(.*)$  %1/%2 [R=301,L]

# Remove .php extension
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/ [NC]
RewriteRule ^(([^/]+/)*[^.]+)\.php$ http://%{HTTP_HOST}/%1$1 [R=301,L]

# Rewrite uppercase letter URL to lowercase with content/httpd.conf RewriteMap lc int:tolower
## Solution without httpd.conf RewriteMap in github.com/laukstein/ajax-seo/wiki/Lowercase-URL
RewriteCond %{REQUEST_URI} .+
RewriteRule ^[^A-Z]*[A-Z] ${lc:%0} [R=301,L]

# Block folder access that begins with a period (like .git, .svn)
RewriteRule "(^|/)\." - [F]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/?([^\./]*)$ ?api&url=$1 [L,QSA]
RewriteRule ^([^.]*)$ ?url=$1 [L,QSA]

RewriteRule .* - [E=HTTP_IF_MODIFIED_SINCE:%{HTTP:If-Modified-Since}]
RewriteRule .* - [E=HTTP_IF_NONE_MATCH:%{HTTP:If-None-Match}]

I've managed to create an exemption to allow the posts.php file to be accessed - However I am clueless on how to allow my pages to access all images stored in the /uploads/ path.

Many thanks, Mike

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

香草可樂 2024-12-16 05:56:21

当您不希望重写 URL 时,请使用 -。文件顶部的这条规则(或者在您想要首先处理的任何其他规则之后)可以解决问题:

RewriteRule ^uploads/ - [L]

Use - for when you don't want the URL to be rewritten. This rule at the top of your file (or after any other rules you want processed first) would do the trick:

RewriteRule ^uploads/ - [L]
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文