我正在尝试创建干净的网址,到目前为止,我已设法使用.htaccess转换以下网址:
www.example.com/index.php?catagory=Section1&page=Page1
成:
www.example.com/Section1/Page1 /
问题是让PHP解释URL并提取变量。这是我正在使用的代码:
.htaccess代码:
Options +FollowSymLinks
Options +Indexes
RewriteEngine on
INTERNAL STATIC URL TO DYNAMIC URL
RewriteRule ^/([^/]+)/?$ /index.php?c=$1 [L]
RewriteRule ^/([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L]
EXTERNAL DYNAMIC URL TO STATIC URL
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagory=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1/? [R=301,L]
RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagroy=([^&]+)&page=([^&]+)\ HTTP/
RewriteRule ^index\.php$ http://www.example.com/%1/%2/? [R=301,L]
PHP代码:
$urlVariables = explode("/",$_SERVER['REQUEST_URI']);
$catagory = $urlVariables[1];
$page = $urlVariables[2];
有了这个,我不断收到404错误页面。如果我将index.php添加到htacces代码的这一行:
RewriteRule ^index\.php$ http://www.example.com/index.php/%1/? [R=301,L]
页面至少加载,但未加载css和图像。
答案 0 :(得分:1)
在.htaccess文件中使用mod_rewrite时,需要从模式中删除上下文本地路径前缀。因此,如果根目录中的.htaccess文件是前导/
:
RewriteRule ^([^/]+)/?$ /index.php?c=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L]