2016-01-24 159 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