2017-04-24 92 views
0

我有專門的服務器運行CENTOS 6.9 x86_64 xen hvm,cPanel & WHM 64.0(build 15),Apache 2.4.25和PHP 5.6.30。Apache htaccess - 如何重寫URL以包含頁面的廣告名稱值?

我的網站的.htaccess文件目前包括以下內容並正在按預期:

RewriteEngine On 
RewriteCond %{HTTPS} on 
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

ErrorDocument 404 /404.php 

由於我沒有經驗與mod_rewrite的和PHP,我需要用以下幫助:

我的網站包含的個人資料不同型號的頁面。 的電流模型輪廓URL的例子被寫成這樣:

http://www.mywebsite.com/profile.php?id=123

我想的URL來代替重新寫爲:

http://www.mywebsite.com/profile/123/model-ad-name-here

爲了顯示(在profile.php頁面中)模型的廣告名稱,我有以下代碼:

 <?php require_once('cms/includes/init_files.php');?> 

     <?php 

     $id = getParam('id'); 
     $query_string = "SELECT * from profiles where id='$id'"; 
     $query = $db->query($query_string); 
     $data = $db->fetch_assoc($query); 

     ?> 

...並顯示profile.php頁面本身的廣告名稱值,我用的是代碼:

 <?php echo ucfirst($data['ad_name']); ?> 

1)我需要添加到.htaccess文件什麼來重新寫入網址以我上面提到的格式和...

2)是否有任何特殊的代碼,我需要編輯/添加到profile.php頁面,以幫助協助?

我很感謝您對此提供的任何專家協助。

回答

0

您可以添加一個重寫規則,如下所示。

這將重寫profile/123/model-ad-name-hereprofile.php?id=123使您的漂亮的url工作。此規則僅適用於數字ID,並且model-ad-name-here將被忽略。

這是非常基本的URL重寫。你可以閱讀URL rewriting for beginnersApache Module mod_rewrite,並在未來自己做。

除了所有指向配置文件的鏈接都必須更新爲新的URL之外,不需要對您進行任何編輯。

RewriteEngine On 

RewriteCond %{HTTPS} on 
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L] 

# This rewrites 'profile/123/model-ad-name-here' to 'profile.php?id=123' 
RewriteRule ^profile/([0-9]+) profile.php?$1 [L]