2016-01-24 158 views
0

我正在嘗試通過更簡潔的URL獲得即興SEO。 下面是我的.htaccess文件:如何使用.htaccess編寫更乾淨的網址

<IfModule mod_rewrite.c> 
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteRule^cleanurl2/podtadquery.php [L] 
</IfModule> 

下面是我的網址目前正在尋找這樣的:

http://localhost/cleanerurl2/podtadquery.php?product=Actuators&subgroup=Electrical

我所試圖實現的是

http://localhost/cleanerurl2/podtadquery.php/Actuators/Electrical

請建議我如何實現這一目標。謝謝adance

+3

http://stackoverflow.com/questions/18851994/how-to-create-clean-url-using-htaccess GOOGLE了它。花了5秒鐘。 –

+0

@DiddleDot它不像我沒有谷歌它或從我的最後工作,我問這個問題之前。感謝這篇文章,但我努力工作我的代碼 –

回答

0

您可以使用下面的代碼在/root/.htaccess

RewriteEngine On 
#1) 
#Redirect from "/cleanerurl2/podtadquery.php?product=foo&subgroup=bar" to "/cleanerurl2/podtadquery.php/foo/bar" 
RewriteCond %{THE_REQUEST} /cleanerurl2/podtadquery\.php\?product=([^&]+)&subgroup=([^&\s]+) [NC] 
RewriteRule^/cleanerurl2/podtadquery.php/%1/%2? [NC,L,R] 
#2) 
#Now, internally redirect "/cleanerurl2/podtadquery.php/foo/bar" to "/cleanerurl2/podtadquery.php?product=foo&subgroup=bar 
RewriteRule ^cleanerurl2/podtadquery\.php/([^/]+)/([^/]+)/?$ /cleanerurl2/podtadquery.php?product=$1&subgroup=$2 [NC,L] 
0

在你的.htaccess文件

RewriteEngine On 
RewriteRule !\.(?:jpe?g|gif|bmp|png|tiff|css|js)$ index.php [L,NC] 

下聯是排除JPG,JPEG,GIF ,bmp,png,tiff,css和js文件。其他的URI將被轉發到index.php。

的index.php

<?php 
$uri = rtrim(dirname($_SERVER["SCRIPT_NAME"]), '/'); 
$uri = trim(str_replace($uri, '', $_SERVER['REQUEST_URI']), '/'); 

echo $uri; 
?> 

如果使用的是Ubuntu的服務器,請在下面做太行

第1步:

sudo a2enmod rewrite 

第2步: 打開apache conf文件

sudo vim /etc/apache2/apache2.conf 

取消註釋該行(約187線約)

AccessFileName .htaccess 

然後找到行,其中有

<Directory /var/www/> 
     Options Indexes FollowSymLinks 
     AllowOverride None 
     Require all granted 
</Directory> 

「無」 替換爲 「全部」

<Directory /var/www/> 
     Options Indexes FollowSymLinks 
     AllowOverride All 
     Require all granted 
</Directory> 

第3步。 重新啓動Apache

service apache2 restart