2015-10-05 101 views
0

我想在這裏重新寫這個URL;htaccess mod_rewrite URL問題

site.com/product.php?s=electricity-at-work-regulations 

site.com/courses/electricity-at-work-regulations 

所以更改product.php到課程和一般整齊的URL設置。

這可能嗎?

.htacess文件

<IfModule mod_rewrite.c> 
     RewriteEngine On 
     RewriteCond %{HTTP_HOST} !^peoplefactor.co.uk$ [NC] 
     RewriteRule ^(.*)$ http://peoplefactor.co.uk/$1 [L,R=301] 
    </IfModule> 


    # Clean URLS 
    <IfModule mod_rewrite.c> 
     Options -MultiViews 
     RewriteEngine On 

     RewriteRule ^blog/([a-zA-Z0-9-/]+)/preview$ /blog/post.php?s=$1&preview=all [L] 
     RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L] 

     RewriteRule ^course/([a-zA-Z0-9-/]+)$ /course.php?s=$1 [L] 

     RewriteCond %{REQUEST_FILENAME} !-d 
     RewriteCond %{REQUEST_FILENAME} !-f 
     RewriteRule ^([^\.]+)$ $1.php [NC,L] 
    </IfModule> 

     # Gets rid of www. 
    <IfModule mod_rewrite.c> 
     RewriteEngine On 


     RewriteCond %{HTTPS} !=on 
     RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC] 
     RewriteRule^http://%1%{REQUEST_URI} [R=301,L] 
    </IfModule> 

回答

1

是的,但它會更好,周圍的其他方法。 您應該始終在您的應用程序中調用site.com/courses/electricity-at-work-regulations並將其重新路由到site.com/product.php?s=electricity-at-work-regulations。類似於

RewriteEngine On 
RewriteBase/
RewriteRule ^(courses)/([a-zA-Z\-]+)$ product.php?s=$2 [L] 
+0

完美,謝謝。 正如您所建議的,我會在應用程序中將其更改爲「課程」。你會碰巧知道清理這個網址的最好方法嗎? 'site.com/courses.php?S =電力-AT-工作regulations' 閱讀 'site.com /場/電-AT-工作regulations'再次 感謝。 – Cameron

+0

從頭開始,應該看起來像RewriteRule ^(courses.php \?s \ =)/([a-zA-Z \ - ] +)$ courses/$ 2 [L]無法測試它 – Eric

+0

這似乎適用於我, '重寫規則^ course /([a-zA-Z0-9 - /] +)$ /course.php?s=$1 [L]'但現在我的css或js文件都不是加工。這很奇怪,因爲當我查看源代碼鏈接時,開發人員工具中沒有錯誤。有任何想法嗎? – Cameron