0

我正在尝试使用 .htaccess 使我的日历网站的 URL 看起来更好,但我无法让它工作。


我已经有一条规则,它删除了 .php 扩展名,而且效果很好。它看起来像这样:

RewriteEngine On
# turn on the mod_rewrite engine

RewriteCond %{REQUEST_FILENAME}.php -f
# IF the request filename with .php extension is a file which exists
RewriteCond %{REQUEST_URI} !/$
# AND the request is not for a directory
RewriteRule (.*) $1\.php [L]
# redirect to the php script with the requested filename

我当前的 URL 如下所示: http ://mydomain.com/calendar/calendar-site?year=2013&month=november

...我想让它看起来像这样: http: //mydomain.com/calendar/2013/November

该站点无需重写即可完美运行,我使用 $_GET[] 获取 URL 中年份和月份的值,但在重写后,它无法从 url 中获取值。


我已经尝试过这些(当然不是同时尝试)

RewriteRule ^calendar/([^/]*)$ /calendar-site.php?year=$1&month=$2 [L]
RewriteRule ^([^/]*)/([^/]*)$ /calendar-site.php?year=$1&month=$2 [L]

第一个创建 404 页面,第二个无法从 url 获取值 + 它弄乱了样式表。


希望你们能在这里帮助我:D

谢谢 - 杰斯珀

4

1 回答 1

1

你很接近,但在你的第一次尝试中,你只包括了一个匹配的组,而不是一个月的匹配组。包含字符类两次以匹配捕获的年份,$1并再次匹配捕获的月份$2。用于RewriteBase设置重定向的根目录。请注意,“月”值的大小写与 URL 中的大小写相同。

RewriteEngine on
RewriteBase /
RewriteRule ^calendar/([^/]*)/([^/]*)$ /calendar/calendar-site.php?year=$1&month=$2 [L]

使用http://htaccess.madewithlove.be/进行测试

input url
http://mydomain.com/calendar/2013/november

output url
http://mydomain.com/calendar/calendar-site.php

debugging info
1 RewriteBase /
2 RewriteRule ^calendar/([^/]*)/([^/]*)$ /calendar/calendar-site.php?year=$1&month=$2 [L]
  This rule was met, the new url is http://mydomain.com/calendar/calendar-site.php
  The tests are stopped because the L in your RewriteRule options
于 2013-11-10T23:57:59.643 回答