partho
partho

Reputation: 1084

Link not opening properly after hiding file extension using htaccess

I have used ht-access to hide .php file extension. Inside .htaccess file my code is :

# Apache Rewrite Rules
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

# Add trailing slash to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/|#(.*))$
RewriteRule ^(.*)$ $1/ [R=301,L]

# Remove .php-extension from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php 

# End of Apache Rewrite Rules
</IfModule>

It's hiding .php file extension well. But problem is that, link inside href attribute is not opening properly.

I have two files:

public_html/afiliate/product_details.php

public_html/afiliate/afiliate_login.php

Inside product_details.php I have given link of afiliate_login.php like this

<a href="afiliate_login.php" target="_blank">Log in</a>

Clicking Log in I should go to a page like

example.com/afiliate/afiliate_login/

But it takes me to

example.com/afiliate/product_details/afiliate_login/

And hence a 404 error

Again notice that, if i manually type this URL

example.com/afiliate/afiliate_login/

that's OK, it goes to the page as expected.

Now how I can make links work properly?

Upvotes: 0

Views: 119

Answers (2)

Anulal S
Anulal S

Reputation: 6625

Previously the browser considered product_details.php as a file. but now its a folder, because its product_details/

one option available here is to update the link's like this

<a href="../afiliate_login.php" target="_blank">Log in</a>

but its always suggested like the link below

<a href="/afiliate_login" target="_blank">Log in</a>

Upvotes: 1

Sourabh Kumar Sharma
Sourabh Kumar Sharma

Reputation: 2817

After this code:

RewriteRule ^([^\.]+)/$ $1.php 

put the below code

RewriteRule afiliate_login$   afiliate_login.php [L]

basically means that any request that comes to afiliate_login will show the data from afiliate_login.php

You got that problem because in your anchor tag you are using relative url. Since your url look like this without the file extension like, example.com/afiliate/afiliate_login/ , affiliate_login/ will be also considered as a directory and the html thinks that the anchor tag is in affiliate_login/ but it in reality it does not exists there so you get 404 error.

Upvotes: 2

Related Questions