繁体   English   中英

如何使用RewriteRule(Apache)亲吻

[英]How to KISS with RewriteRule (Apache)

我正在编写一个小型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

也:

  1. 它一定不能阻止现有文件夹:(该部分可以很容易地编写脚本,以在应用程序的根目录中为每个文件夹添加重写规则。)

    • 如果有一个名为image的目录,并且我使用http://host/image/test.png浏览,我将访问image,并且不应在$ _GET ['title']变量中获取/image/test.png。
  2. 它不能阻止实际文件,如果我想要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 /示例不起作用,并且我觉得我走的很糟糕……我重复一遍,我不想在PHP中处理它。 我确实认为重写在这里对我来说更灵活。

非常感谢您的帮助和指导。

希望我的英语对我有好处,因为它不是我的母语。

如果可以解决title的问题。 那么我建议更改最后一行:

RewriteRule ^([^_/][^\.]*)(/?$) index.php?title=$1 [L,QSA]

编辑:最后一条规则几乎匹配所有不带“ _”的内容,因此您需要进行一些常规的exp匹配,以使其理解其文件。

如果文件/文件夹不存在,则重定向.htaccess如果文件/文件夹不存在,则重定向

(您可以将其与重写器结合使用(否_)


固定

显然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]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM