[英]Issues with .htaccess file to make SEO Friendly URLs
我是Web開發的新手,我正在嘗試使用目錄根目錄下的.htaccess文件創建SEO友好的URL。 我已經修改了apache2.conf文件,使其包含:
<Directory /home/sakyun/sites/> (Being my file directory that i changed)
Options Indexes FollowSymLinks
AllowOverride All (So that my .htaccess file is read)
Require all granted
</Directory>
我已經啟用了重寫mod:
sudo a2enmod rewrite
我用以下命令重新啟動了Apache:
sudo service apache2 restart
我的.htaccess位於目錄的根目錄中,包含以下內容:
# Turn Rewrite Engine On
RewriteEngine on
# Rewrite for profile.php?Id=1&Name=Name
RewriteRule ^profile/([0-9]+)/([0-9a-zA-Z_-]+)$ php/profile.php?Id=$1&Name=$2 [NC,L]
所以對我來說,似乎一切正確,但是當我加載頁面時,我的網址仍然是這樣的:
localhost/abc/php/profile.php?Id=1&Name=Name
我期望更多的地方:
localhost/abc/profile/1/Name
我也正在虛擬機中運行UBUNTU 16.04,不確定是否有幫助。
我究竟做錯了什么?
apache甚至考慮到我的.htaccess文件嗎?
如果沒有,我該怎么做才能使它正常工作?
以下RewriteRule
僅在內部重寫URL。
RewriteRule ^/?abc/profile/([0-9]+)/([0-9a-zA-Z_-]+)$ /abc/php/profile.php?Id=$1&Name=$2 [NC,L]
當用戶請求localhost/abc/profile/1/Name
,顯示的URL瀏覽器是localhost/abc/profile/1/Name
,他們實際上正在查看頁面localhost/abc/php/profile.php?Id=1&Name=Name
。
您必須添加另一個RewriteRule
才能將用戶重定向到SEO友好URL。
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/abc/php/profile.php [NC]
RewriteCond %{QUERY_STRING} Id=([^&]+)&Name=([^&]+)
RewriteRule ^/?abc/php/profile.php$ /abc/profile/%1/%2? [R=301,L]
它檢查用戶是否請求URL localhost/abc/php/profile.php
,以及是否包含查詢字符串Id
和Name
,將用戶重定向到SEO友好URL。 %1
是對第一個([^&]+)
反向引用,而%2
是對第二個([^&]+)
反向引用,匹配與&
以外的所有字符。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.