我正在为一个客户开发项目,该客户的博客/新闻页面是他们网站的一部分。这很好,但他们要求他们有自定义固定链接而不是标准问题php变量。
我在searchfriendlyurls.com上使用了生成器来创建重写规则。我已将此添加到我的.htaccess文件中,该文件位于网站的根文件夹中,但是当我点击链接时,它只是将我发送到我的主机404错误页面。有什么建议。 .htaccess文件如下:
Options -Multiviews
RewriteEngine On
RewriteBase /
# Force search engines to use domain.example.org.uk
RewriteCond %{HTTP_HOST} !^domain\.example\.org\.uk$
RewriteRule ^(.*) http://domain.example.org.uk/$1 [R=301,L]
# Specify search friendly URLs
RewriteRule ^media/news/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]
# Generated for free at SearchFriendlyURLs.com
非常感谢任何建议
答案 0 :(得分:0)
我注意到你的article.php可能会期待numbers
根据你的查询判断 article.php?article_url_year=
$ 1& article_url_month=
$ 2 但是你的RewriteRule RewriteRule ^ media / news / ([a-z]+)
只会匹配字母。
尝试替换此行:
RewriteRule ^media/news/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]
有了这个:
RewriteRule ^media/news/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)/([a-zA-Z0-9-]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]
看看它是否有效。
此代码仅允许:
答案 1 :(得分:0)
在混音中添加另一个正则表达式变体:
RewriteRule ^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$ /media/news/article.php?article_url_year=$1&article_url_month=$2&article_url_title=$3&article_id=$4 [L]
e.g。对于URL路径:/ media / news / 2012/11 / test-article / 1
重写日志(如果启用)将显示:
[rid#7f920b4b00a0/initial] (3) [perdir /srv/www/htdocs/] applying pattern '^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$' to uri 'media/news/2012/11/test-article/1'
[rid#7f920b4b00a0/initial] (2) [perdir /srv/www/htdocs/] rewrite 'media/news/2012/11/test-article/1' -> '/media/news/article.php?article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1'
[rid#7f920b4b00a0/initial] (3) split uri=/media/news/article.php?article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1 -> uri=/media/news/article.php, args=article_url_year=2012&article_url_month=11&article_url_title=test-article&article_id=1
[rid#7f920b4b00a0/initial] (2) [perdir /srv/www/htdocs/] trying to replace prefix /srv/www/htdocs/ with /
[rid#7f920b4b00a0/initial] (1) [perdir /srv/www/htdocs/] internal redirect with /media/news/article.php [INTERNAL REDIRECT]
[rid#7f920bcdda00/initial/redir#1] (3) [perdir /srv/www/htdocs/] strip per-dir prefix: /srv/www/htdocs/media/news/article.php -> media/news/article.php
[rid#7f920bcdda00/initial/redir#1] (3) [perdir /srv/www/htdocs/] applying pattern '^media/news/([^/]+)/([^/]+)/([^/]+)/([^/]+)$' to uri 'media/news/article.php'
[rid#7f920bcdda00/initial/redir#1] (1) [perdir /srv/www/htdocs/] pass through /srv/www/htdocs/media/news/article.php