简体   繁体   English

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

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

I'm trying to create clean URLs and so far I've managed to use .htaccess to convert the following URL: 我正在尝试创建干净的URL,到目前为止,我已经设法使用.htaccess来转换以下URL:

www.example.com/index.php?catagory=Section1&page=Page1 www.example.com/index.php?catagory=Section1&page=Page1

into: 变成:

www.example.com/Section1/Page1/ www.example.com/Section1/Page1/

The problem is then getting PHP to interpret the URL and extract the variables. 然后问题是使PHP解释URL并提取变量。 Here's the code I'm using: 这是我正在使用的代码:

.htaccess code: .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 code: PHP代码:

$urlVariables = explode("/",$_SERVER['REQUEST_URI']);
$catagory = $urlVariables[1];
$page = $urlVariables[2];

With this I keep getting a 404 error page. 有了这个,我不断得到404错误页面。 If I add index.php into this line of the htacces code: 如果将index.php添加到htacces代码的这一行:

RewriteRule ^index\.php$ http://www.example.com/index.php/%1/? [R=301,L]

The page at least loads, but the css and images are not loaded. 该页面至少已加载,但未加载CSS和图像。

When using mod_rewrite in a .htaccess file, you need to remove the contextual local path prefix from the pattern. 在.htaccess文件中使用mod_rewrite时,您需要从模式中删除上下文本地路径前缀。 So in case of the .htaccess file in the root directory the leading / : 因此,在根目录中的.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