2017-02-28 63 views
1

我有一個非常簡單的要求。.htaccess和php GET

所有.php擴展名必須拆除和我的GET請求的URL

http://domain.com/product.php?name=<product_name> 

應該成爲

http://domain.com/product/<product_name> 

我曾嘗試以下:

RewriteEngine On 

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^home/?$ index.php [NC,L] 
RewriteRule ^product/([A-Za-z]+)/?$ product.php?name=$1 [L] 

RewriteRule ^([^\.]+)$ $1.php 
RewriteRule ^([^\.]+)$ $1.html [NC,L] 

當我這樣做所有.php & .html擴展名都被刪除,但GET頁面的css和js正在打破

試過這也:

RewriteRule ^([A-Za-z]+)/?$ product.php?name=$1 [L] 

然後php擴展顯示或者轉到GET請求頁面。

我是.htaccess的新手。現在打破我的頭!

+0

在您的獲取頁面中爲js和css提供完整的URL。 – Ruby

+0

是的,我正在考慮使用* *。這工作!但不知道這是否是正確的做/使用! – Ashwin

回答

0

嘗試以下操作:

<IfModule mod_rewrite.c> 
    Options +FollowSymLinks -MultiViews 
    # Turn mod_rewrite on 
    RewriteEngine On 
    RewriteBase/

    ## If the request is for a valid directory 
    RewriteCond %{REQUEST_FILENAME} -d [OR] 
    ## If the request is for a valid file 
    RewriteCond %{REQUEST_FILENAME} -f [OR] 
    ## If the request is for a valid link 
    RewriteCond %{REQUEST_FILENAME} -l 
    ## don't do anything 
    RewriteRule^- [L] 

    # if requested files exists in /public then forward it there 
    RewriteCond %{DOCUMENT_ROOT}/public/$1 -f 
    RewriteRule ^(.+?)/?$ /public/$1 [L] 

    # if requested files doesn't exist in /public then forward it to requested php file 
    RewriteCond %{DOCUMENT_ROOT}/$1 !-f 
    RewriteRule ^(.+?)/?$ %{REQUEST_FILENAME}.php [L] 
</IfModule> 
+0

嘗試了/代碼的第一部分的代碼/不工作 – Ashwin

0

使用此在.htaccess文件:

RewriteEngine On 
RewriteRule ^product/([^/]*)$ /product.php?name=$1 [L] 

確保您清除緩存測試在此之前,它應該留給你的網址:http://domain.com/product/<product_name>

+0

CSS/JS/img的休息! – Ashwin