Problem using URL rewrite (Relative Paths not work

2019-01-28 06:02发布

问题:

I am using a .htccess file in order to rewrite my URLs. I am using the following rules in my .htaccess files

Options +FollowSymlinks
RewriteEngine on 
RewriteBase /
RewriteRule ^product/([0-9]+)/([A-Za-z0-9+]+)$ /product.php?productid=$1&prodname=$2

The rewrite worked fine.

But when i try to access any other page through relative path which is in all the cases, the URL is not getting redirected properly.

Like in the below example

<form action "something.php" method="post">
   <input type="text" name="1" />
   <input type="submit" value="submit" />
</form>

When I click on submit, the page that gets loaded is http://mydomain/product/1/something/something.php which does not exist and hence throws a 404 error

I tried using the base tag in the head but this creates problem while I am using page anchors and modal windows. It gets redirected to the index page.

Please let me know if further details are required.

I am fairly new to this. So, could someone please help me with this?

Thanks in Advance, Kartik

回答1:

This is a URL resolving issue. See mod_rewrite URL info required for details.

You can either use absolute URL paths or absolute URLs:

<form action="/something.php" method="post">
<form action="http://example.com/something.php" method="post">

Or you change the base URL (see BASE HTML element) so that every relative URL is resolved from that URL and not the current URL:

<base href="/">
<base href="http://example.com/">


回答2:

The short and easy version of solving this is to absolute-path your URLs - /something.php in the form action, not just 'something.php'.