Search code examples
.htaccessurlrewriting

Use same variable name in htaccess for rewriting PHP page


I have two pages index.php and page_detail.php, I'm using the following code in .htaccess for rewriting:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?cat=$1&title=$2&pageId=$3&id=$4 [L]
RewriteBase /
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\.html$ /page_detail.php?countryname=$1&country=$2&id=$3 [L]

URL rewriting is working good when I am on the index.php page. When as I move to detail_page.php then every href shows after page URL the following:

First page URL (good working): example.com/country/586/3.html

Trouble in: example.com/country/586/country/586/3.html

HTML link code:

<a href="<?=country_name($con,$row['country'])?>/<?=$row['country']?>/<?=$row['id']‌​?>.html">

Solution

  • You either need to make the links look like this:

    <a href="/<?=country_name($con,$row['country'])?>/<?=$row['country']?>/<?=$row['id']‌​?>.html">
    

    (note the leading /)

    or add this to the header of your pages:

    <base href="/" />