我有一个文件http://sub.domain.com/apush/noteGetter.php
,可以处理d-m-y
到noteGetter.php?date=d-m-y
格式的日期。例如,我将使用2017年1月13日。我希望网址为http://sub.domain.com/apush/notes/2017/01/13
。我知道这可以通过htaccess使用mod_rewrite来完成,但我对正则表达式的经验很少,而对于htaccess则更少。我需要添加哪些规则和条件才能重写此URL?
更新
当前' apush'目录.htaccess
RewriteEngine on
RewriteRule ^notes/(\d{4})/(\d{2})/(\d{2})$ "noteGetter.php?date=$3-$2-$1"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [L]
DirectoryIndex home.php
ErrorDocument 404 'https://www.domain.com/404'
当前' root'目录.htaccess
AuthType Basic
AuthUserFile /home/tsenter/sub.domain.com/.htpasswd
AuthName "Members Area"
require valid-user
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^notes/(\d{4})/(\d{2})/(\d{2})$ "noteGetter.php?date=$3-$2-$1"
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
ErrorDocument 404 'https://www.domain.com/404'
更新2
新的' apush'目录.htaccess
RewriteEngine on
RewriteRule ^notes/(\d{4})/(\d{2})/(\d{2})$ "noteGetter.php?date=$3-$2-$1"
新的' root'目录.htaccess
AuthType Basic
AuthUserFile /home/tsenter/sub.domain.com/.htpasswd
AuthName "Members Area"
require valid-user
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
DirectoryIndex home.php
ErrorDocument 404 'https://www.domain.com/404'
答案 0 :(得分:2)
我从讨论中了解到,您希望将规则放在.htaccess
目录中的apush
文件中。您需要在apush
.htaccess
文件中使用此功能:
RewriteEngine on
RewriteRule ^notes/(\d{4})/(\d{2})/(\d{2})$ "noteGetter.php?date=$3-$2-$1"
这是一年中任意四个数字,月份任意两个数字,一天任意两个数字的简单匹配。最简单的方法是在脚本中进行验证,并在此处使用简单的正则表达式。