htaccess重写规则将目录/名称重写为index.php?id

时间:2014-01-10 10:17:38

标签: php .htaccess

我在htaccess文件中使用此代码:

# Rewrites /blog/post-name-1 to be /index.php?post_name=post-name-1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9-]+)/?$ /index.php?id=blog&post_name=$1 [L,QSA]

我想重写/blog/post-name-1以查看/index.php?id=blog&post_name=post-name-1

但显示404 Page Not Found

2 个答案:

答案 0 :(得分:1)

认为您需要关注:

RewriteEngine On
# Rewrites /blog/post-name-2 to be /index.php?post_name=post-name-2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /index.php?id=$1&post_name=$2 [QSA,L]

答案 1 :(得分:0)

尝试:

RewriteEngine On
# Rewrites /blog/post-name-1 to be /index.php?post_name=post-name-1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?id=blog&post_name=$1 [QSA,L]

同时确保以下内容;

  • 您已允许.htaccess成为httpd.conf
  • 上的覆盖文件
  • 您已在httpd.conf
  • 中将AllowOverride设置为ALL