我们如何重写<a href=""> urls using .htaccess

时间:2016-04-04 06:00:55

标签: php .htaccess mod-rewrite url-rewriting url-routing

I am having <a href> tags on website and user go to next page with parameters added to urls depending on parameters selected from < ahref> . The difference in my problem is on my website when user clicks on <a href> url it should be rewritten to seo friendly url

For watching it live you can see here -

url:- expertsusa.org/page1.php?id=4&name=Medical-Category is not rewritten to expertsusa.org/page1/Medical-Category and sometimes it is successfully rewritten but shown 404 Page and weird thing is both .htaccess file and page1.php file are in same root folder of website

Example -

<a href="page1.php?param=4&name=Medical-Category">Medical Category</a>`

You can see in this paramter i am passing values through parameters. I want to rewrite this to simple seo form like below -

Url - www.example.com/page1/Medical-Category

Following is the mod_rewrite Module Code in my .htaccess file

ErrorDocument 404 default 
<IfModule mod_rewrite.c>

Options -MultiViews
RewriteEngine On

#RewriteRule ^ /%1/%2? [R=302,L,NE]
#RewriteRule ^(page1)/([\w-]+)/?$ $1.php?id=4&name=$2 [L,NC,QSA]


RewriteRule  ^/ViewCategories/([0-9]+)/(.*)/(.*)/(.*)$  ViewCategories.php?param=$1
RewriteRule    ^/Add-Expert-Listing/$1   Add-Expert-Listing.php    [NC,L]   
RewriteRule    ^/SearchResult/$1   SearchResult.php    [NC,L]    
RewriteRule    ^/Pricing/$1   Pricing.php    [NC,L]   
RewriteRule    ^/My-Lists/$1   MyLists.php    [NC,L]   
RewriteRule    ^/My-Package/$1   MyPackage.php    [NC,L]   
RewriteRule    ^/Change-Password/$1   Change-Password.php    [NC,L]   
RewriteRule    ^/faq/$1   faq.php    [NC,L]   
RewriteRule    ^/ContactUs/$1   ContactUs.php    [NC,L]   
RewriteRule    ^/ViewCategories/$1   ViewCategories.php    [NC,L]   
RewriteRule    ^/Profile/$1   Profile.php    [NC,L]   
</IfModule>

1 个答案:

答案 0 :(得分:0)

您可以在站点根目录中使用这些规则.htaccess:

Options -MultiViews
RewriteEngine On

RewriteCond %{THE_REQUEST} /(page1)\.php\?id=4&name=([^\s&]+)  [NC]
RewriteRule ^ /%1/%2? [R=302,L,NE]

RewriteRule ^(page1)/([\w-]+)/?$ $1.php?id=4&name=$2 [L,NC,QSA]