簡體   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