繁体   English   中英

使PHP读取.htacces创建的干净URL

[英]Getting PHP to read the clean URL created by .htacces

我正在尝试创建干净的URL,到目前为止,我已经设法使用.htaccess来转换以下URL:

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和图像。

在.htaccess文件中使用mod_rewrite时,您需要从模式中删除上下文本地路径前缀。 因此,在根目录中的.htaccess文件中,前导/

RewriteRule ^([^/]+)/?$ /index.php?c=$1 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM