我正在编写一个小的Web应用程序,我需要做一些重写规则,因为我很懒,不想在PHP中这样做。 而且我看到自己写了很多规则,可能没什么,所以我寻求你的意见和帮助!
- YEAR is 4 digits
- MONTH is two digits
- DAY is two digits
- TITLE is a long word including -, like : i-am-a-title
- NB_PAGE is only number (no limit here)
- The trailing slash is not mandatory
1. /YEAR/MONTH/DAY/title => index.php?title=TITLE
2. /page/NB_PAGE => index.php?page=NB_PAGE
3. /YEAR/MONTH/DAY/ => index.php?year=YEAR&month=MONTH&day=DAY
4. /YEAR/MONTH/DAY/page/NB_PAGE => index.php?year=YEAR&month=MONTH&day=DAY&page=NB_PAGE
5. /YEAR/MONTH/ => index.php?year=YEAR&month=MONTH
6. /YEAR/MONTH/page/NB_PAGE => index.php?year=YEAR&month=MONTH&page=NB_PAGE
7. /YEAR/ => index.php?year=YEAR
8. /YEAR/page/NB_PAGE => index.php?year=YEAR&page=NB_PAGE
9. /archives/ => _cache/archive.html
10. /feed/ => /_atom.xml
11. Everything NOT starting with a _ is considered a title and will redir to index.php?title=TITLE
另外:
它不能阻止现有文件夹:(这部分可以很容易编写脚本,为应用程序根目录下的每个文件夹添加重写规则。)
它不能阻止真实文件,如果我想要http://host/file-that-exists.txt,我应该拥有它。如果文件在目录中,请参阅下面的注释。
其实我带来了这个:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*).php(/?$) $1.php [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(\w*\b)(/?$) index.php?title=$4 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})(/?$) index.php?year=$1&month=$2&day=$3 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]+)(/?$) index.php?year=$1&month=$2&day=$3&page=$4 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})(/?$) index.php?year=$1&month=$2 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/page/([0-9]+)(/?$) index.php?year=$1&month=$2&page=$3 [L,QSA]
RewriteRule ^([0-9]{4})(/?$) index.php?year=$1 [L,QSA]
RewriteRule ^([0-9]{4})/page/([0-9]+)(/?$) index.php?year=$1&page=$2 [L,QSA]
RewriteRule ^(archives\b)(/?$) _cache/archive.html [L,QSA]
RewriteRule ^(feed\b)(/?$) /_atom.xml [L,QSA]
RewriteRule ^page/([0-9]+)(/?$) index.php?page=$1 [L,QSA]
RewriteRule ^([^_/].*)(/?$) index.php?title=$1 [L,QSA]
</IfModule>
但它不适用于/ image / example而且我觉得我正在走上一条糟糕的道路......我再说一遍,我不想在PHP中处理这个问题。我确实认为rewriterule对我来说更灵活。
非常感谢您的帮助和指导。
希望我的英语不错,因为它不是我的母语。
答案 0 :(得分:0)
如果你可以在没有标题的情况下应对。那么我建议改变最后一行:
RewriteRule ^([^_/][^\.]*)(/?$) index.php?title=$1 [L,QSA]
编辑:最后一条规则几乎与所有没有“_”的内容匹配,因此您需要提供一些可以理解其文件的常规exp匹配。
答案 1 :(得分:0)
如果文件/文件夹不存在则重定向: .htaccess redirect if file doesn't exist
(你可以将它与你的重写(没有_)
结合起来显然mod_rewrite不处理组名和非捕获组
直到你接受一些答案,这是一个演示(稍后将编辑):http://zaliczam.pl/test/2000/13/75/page/1
http://zaliczam.pl/test/0000/00/00/page/0/?like=a%20g6
RewriteEngine On
RewriteRule ^([0-9]{4})(/([0-9]{2})(/([0-9]{2}))?)?(/page/([0-9]+))?/?\s*$ index.php?year=$1&month=$3&day=$5&page=$7 [L,QSA]
为什么不抓住所有那些
^(?<year>[0-9]{4})(/(?<month>[0-9]{2})(/(?<day>[0-9]{2}))?)?(/page/(?<page>[0-9]+))?/?\s*$
index.php?year=$year&month=$month&day=$day&page=$page
匹配
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/(\w*\b)(/?$) index.php?title=$4 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})(/?$) index.php?year=$1&month=$2&day=$3 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/([0-9]{2})/page/([0-9]+)(/?$) index.php?year=$1&month=$2&day=$3&page=$4 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})(/?$) index.php?year=$1&month=$2 [L,QSA]
RewriteRule ^([0-9]{4})/([0-9]{2})/page/([0-9]+)(/?$) index.php?year=$1&month=$2&page=$3 [L,QSA]
RewriteRule ^([0-9]{4})(/?$) index.php?year=$1 [L,QSA]
RewriteRule ^([0-9]{4})/page/([0-9]+)(/?$) index.php?year=$1&page=$2 [L,QSA]