我的网站运行了一个脚本-> WSS壁纸脚本

我的问题->我一直在试图强制删除或在URL末尾添加斜杠以防止重复的内容并清理我的URL。

我尝试了各种尝试,并尝试了所有可以想到的东西并从Internet上加载了这些东西,但是还没有运气!这可能是一个快速修复,但是我已经看了太多了,所以我可能看不到一些显而易见的东西。

所以我向大家介绍我所有的.htaccess代码:

DirectoryIndex index.php

RewriteEngine on
RewriteRule ^download/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=&width=&height=&cropratio=&download=1 [L]
RewriteRule ^file/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) image.php?id=&width=&height=&cropratio= [L]
RewriteRule ^preview/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+) wallpaper_preview.php?id=&width=&height=&name= [L]
RewriteRule ^thumbnail/([0-9]+)?/([0-9]+)x([0-9]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/]+) image.php?wallpaper_id=&width=&height=&cropratio=&align=&valign=&file= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/p([0-9]+) index.php?task=category&id=&name=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)/p([0-9]+) index.php?task=category&id=&name=&sortby=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+)/([0-9a-zA-Z?-]+)-([0-9]+) index.php?task=category&id=&sortby=&page= [L]
RewriteRule ^cat/([0-9]+)?/([^/\.]+) index.php?task=category&id=&name= [L]
RewriteRule ^tag/([^/\.]+)/([0-9a-zA-Z?-]+)/([0-9]+) index.php?task=tag&t=&sortby=&page= [L]
RewriteRule ^tag/([^/\.]+) index.php?task=tag&t= [L]
RewriteRule ^profile/([0-9]+)?/([^/\.]+) index.php?task=profile&id=&name= [L]
RewriteRule ^profile/comments/([0-9]+)?/([^/\.]+) index.php?task=users_comments&id=&name= [L]
RewriteRule ^page/([0-9]+) index.php?task=view_page&id= [L]  
RewriteRule ^register index.php?task=register [L] 
RewriteRule ^lost-password index.php?task=lost_pass [L] 
RewriteRule ^links index.php?task=links [L]
RewriteRule ^news/item/([0-9]+)/([^/\.]+) index.php?task=news&id= [L]
RewriteRule ^news/page([0-9]+) index.php?task=news&page= [L]
RewriteRule ^members/([^/\.]+)-([^/\.]+)/page([0-9]+)? index.php?task=member_list&sort=&order=&page= [L]
RewriteRule ^members index.php?task=member_list [L]
RewriteRule ^messages index.php?task=messages [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^search/([^/\.]+) index.php?task=search&q= [L]
RewriteRule ^search index.php?task=search [L]
RewriteRule ^submit index.php?task=submit [L]
RewriteRule ^r-([0-9]+)?-([0-9]+)? go.php?id=&ref= [L]
RewriteRule ^r-([0-9]+)? go.php?id= [L]
RewriteRule ^([^/\.]+)/([0-9]+)/([^/\.]+) index.php?task=view&id=&name= [L]
RewriteRule ^news/([^/\.]+) index.php?task=news&name= [L]
RewriteRule ^profile/([^/\.]+) index.php?task=profile&name= [L]
RewriteRule ^news index.php?task=news [L]
RewriteRule ^page/([^/\.]+) index.php?task=view_page&name= [L]  
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([0-9a-zA-Z'?-]+)/([0-9]+) index.php?task=category&name=&sortby=&page= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+)/([^/\.]+) index.php?task=view&name= [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/\.]+) index.php?task=category&name= [L]


## www reslove ##
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/ [R=301,L]
## www reslove ##

## index reslove ##
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://www.epicwallpaper.net/ [R=301,L]
## index reslove ##


评论

另外,如果有人可以修复这个坏男孩,您可以在这里发表回复吗?谢谢

请在此处发布代码。

编辑您的信息,将代码粘贴到您的信息中,突出显示,然后按“ CTRL + K”将其设置为代码格式

你真的有吗?在您的URI中允许? RewriteRules之一(在底部附近)建议如此。我担心它们甚至在.htaccess可以处理它们之前就可能使服务器感到困惑。

#1 楼

RewriteEngine On线的正下方,添加:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ / [L,R] # <- for test, for prod use [L,R=301]


,以强制执行无尾切斜线策略。

要强制执行斜杠策略:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ // [L,R] # <- for test, for prod use [L,R=301]



编辑:评论了R=301的部分,因为,如注释中所述:


注意R=301!将其放置在其中会使许多浏览器无限期地缓存.htaccess文件:如果您不能在所有打开它的机器上清除浏览器缓存,它就会以某种方式不可逆转。测试时,最好使用简单的RR=302


完成测试后,可以使用R=301

评论


上帝,我可以吻你!与强制执行不拖尾斜线一起工作,另一个给了我500服务器错误,谢谢乔恩·林!

–卢克
2014年1月28日在21:59

如果网站位于目录中(例如example.org/blog/),则no-trailing-slash规则不起作用

–Gras Double
2014年12月3日,下午2:59

请注意,R = 301!将其放置在其中会使许多浏览器无限期地缓存.htaccess文件:如果您不能在所有打开它的机器上清除浏览器缓存,它就会以某种方式不可逆转。测试时,最好使用简单的“ R”或“ R = 302”

–乔纳森·韦伯
16-09-22在18:33

关闭VirtualDocumentRoot时,请在开始处删除斜杠,即:$ 1代替/ $ 1并且@JonathanWeber不会提供.htaccess文件,但是expires标头告诉浏览器将资源缓存多长时间。

–twicejr
17-2-22在18:37



@Nrc,因为除了文件和目录之外还有其他类型的映射,并且请求也有可能既不映射到文件也不映射到目录,而是需要重写。否定项确保跳过已重写或已映射到文件系统中某些内容的内容。

–林俊杰
17年4月30日在0:00

#2 楼

为了补充乔恩·林的答案,如果网站位于目录中(例如example.org/blog/),这是一种无尾斜杠技术也可以使用:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [R=301,L]



为了完整起见,这里有一个替代方法强调REQUEST_URI以斜杠开头(至少在.htaccess文件中):

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*)/$
RewriteRule ^ /%1 [R=301,L] <-- added slash here too, don't forget it


不要使用%{REQUEST_URI} (.*)/$。因为在根目录中,REQUEST_URI等于前斜杠/,并且会被误认为是斜杠。


如果您对更多阅读感兴趣,请:


Laravel的PR 3145
关于提交343c31e的讨论

(更新:该技术现已在Laravel 5.5中实现)

评论


这个特殊的变体非常有用,尤其是在使用现代URL约定时。希望您不要介意,我正在将其提交到htaccess代码段存储库。

– alexw
15年3月30日在4:54

这消除了当REQUEST_URI(根)解释为尾部斜线也会引起无限循环时的问题。谢谢

–罗斯兰·阿布赞特(Ruslan Abuzant)
16-2-28在17:26

是的,如果网站位于目录中,则可接受的答案不起作用,但是带有RewriteCond%{REQUEST_URI}(。+)/ $的答案仅删除最后一个斜杠(例如,从sth ///到sth //的多次重定向)某物/某物)。要在一个重定向中删除所有结尾的斜杠,请使用更好的解决方案RewriteCond%{REQUEST_URI}(。+?)/ + $

–mikep
17年5月11日在18:01

我也考虑过这一点。尽管结果是正确的,但考虑到这是一个极端的情况,我更喜欢代码简单。

–Gras Double
17年5月13日在16:22



@GrasDouble,感谢您长期以来一直在寻找这种解决方案。尝试了无数种方法,但您的工作正常!非常感谢你。

–user2711639
17年7月20日在19:38

#3 楼

这就是我用于最新应用程序的内容。

# redirect the main page to landing
##RedirectMatch 302 ^/$ /landing

# remove php ext from url
# https://stackoverflow.com/questions/4026021/remove-php-extension-with-htaccess
RewriteEngine on 

# File exists but has a trailing slash
# https://stackoverflow.com/questions/21417263/htaccess-add-remove-trailing-slash-from-url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)/+$ / [R=302,L,QSA]

# ok. It will still find the file but relative assets won't load
# e.g. page: /landing/  -> assets/js/main.js/main
# that's we have the rules above.
RewriteCond %{REQUEST_FILENAME} !\.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^/?(.*?)/?$ .php


#4 楼

Options +FollowSymLinks
RewriteEngine On
RewriteBase /
## hide .html extension
# To externally redirect /dir/foo.html to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+).html
RewriteRule ^ %1 [R=301,L]

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)/\s
RewriteRule ^ %1 [R=301,L]

## To internally redirect /dir/foo to /dir/foo.html
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^\.]+)$ .html [L]


<Files ~"^.*\.([Hh][Tt][Aa])">
order allow,deny
deny from all
satisfy all
</Files>


如果您补充了html代码或php,则会删除它。允许您添加尾部斜杠,并且在不使用尾部斜杠的情况下也添加了网址,而所有这些都绕过了404代码。再加上一点安全性。

评论


嗨,您如何为nginx编写此代码?我尝试在以下转换器中运行它:winginx.com/en/htaccess,但是此结果使我陷入无限循环:#nginx配置位置/ {重写^(。*)$ /%1重定向;重写^(。*)$ /%1重定向;重写^ /([[^ \。] +)$ /$1.html break; } location〜^。* \。([[Hh] [Tt] [Aa]){拒绝全部; }

–艾尔
17年1月10日在22:12