PDA

View Full Version : Url Rewrite working as directory



george12
06-08-2017, 11:12 AM
I am using URL rewrite for my e-commerce project. I have a url like localhost/norres/category.php?category=1 and I want to change above url in localhost/norres/category/1

I am using .htaccess file and the code is:


RewriteEngine On
RewriteRule ^category/(.+)$ category.php?category=$1

It is working fine but in URL http://localhost/norres/category/1 my href tag is - ">Category

category/ is working as directory and I don't have such folder, so how to resolve? Please help:(

jscheuer1
06-08-2017, 02:33 PM
Have you tried:


RewriteEngine On
RewriteRule ^category/(.+)$ category.php?category=$1
RewriteRule ^category$ category.php

or:


RewriteEngine On
RewriteRule ^category/(.+)$ category.php?category=$1
RewriteRule ^category$ category.php
RewriteRule ^category/$ category.php

Both work for me, depending upon what you're after. The second one gets both category and category/

george12
06-09-2017, 11:01 AM
Yes sir, Actually the URL is successfully rewriting but the problem is that, the category/ works for me as a folder ..
The a href code is <a href="category/<?php echo $category[$i];?>">test</a>
and my HTaccess file code is -

RewriteEngine On
RewriteRule ^category/(.+)$ category.php?category=$1
RewriteRule ^category$ category.php
RewriteRule ^category/$ category.php

The moment when i click on anchor tag my WEB URL becomes - http://localhost/norres/category/Industrial-Hoses
But it is not loading the js, img, css file and php include files
Here is my screen Shot - 6180

jscheuer1
06-09-2017, 11:56 AM
That would seem to be a problem with the PHP code. I made up a test page:

category.php:


<pre>
<?php
print_r( $_REQUEST );
?>
</pre>
<br>Category Page

If I go to: http://localhost/demos/tidbits/rewrite/category or to: http://localhost/demos/tidbits/rewrite/category/ - I see:


Array
(
)

Category Page

If I go to: http://localhost/demos/tidbits/rewrite/category/5 - I see:


Array
(
[category] => 5
)

Category Page

So you see the value is being passed as if the URL were: http://localhost/demos/tidbits/rewrite/category.php?category=5

Any problems are likely caused by there being an error in the code on the PHP page (on category.php). What happens if you go to:

http://localhost/norres/category.php?category=Industrial-Hoses

? If I do the equivalent: http://localhost/demos/tidbits/rewrite/category.php?category=5 I get the same result as: http://localhost/demos/tidbits/rewrite/category/5 - so, if you do not, the rewrites are not working. If you do get the same result, then it's the code on the PHP page.

Now, on a localhost server such as WAMP or XAMP the rewrite engine is turned off by default. That might be the problem, if so, look here:

http://www.anmsaiful.net/blog/php/enable-apache-rewrite-module.html

george12
06-12-2017, 09:49 AM
I have checked my php code and it's working nicely..
The problem is that css , img , js and include files are not loading after rewrite...
I am working on it since last month but got negative result always.
So is it possible for you to provide me PHP, html,htaccess code
So that i can implement on my website..
Thanks in advance

jscheuer1
06-12-2017, 04:42 PM
You have to use the absolute path or, if using a relative path, use one relative to the URL, not the rewritten URL. So, if you have a script in the same folder as the page, you might have something like:


<script src="encode.js"></script>

Well, the server will be looking for that in the non-existing category folder. But if you go like:


<script src="../encode.js"></script>

it will find it in the http://localhost/norres/ folder.

Or you could use the absolute path (probably the best solution), ex (in your case with what you've told me):


<script src="http://localhost/norres/encode.js"></script>

I don't see any problem with includes* though, which should imply that all server resources accessed directly via PHP should work as if the page were not redirected. Images would have the same issues as scripts though. In fact any HTML resources (images, css files, javascript files, videos, etc.) will see the the location.href as the address in the browser's address bar and will need to have their URLs either made absolute or made relative to the address in the address bar. The absolute URL is probably the easiest, and will work no matter how the page is redirected in the future and will work even if the page is accessed without using the redirect.

A trick that might work would be to put this in the head of the HTML portion of the page, before any relative URL's:


<base href="<?php echo $_SERVER['PHP_SELF']; ?>">

Whatever method you use though, there could still be other problems for anything that's missed one way or the other. Like even with the base href method, if an external script references the location.href property in any important way that depends upon the path without accommodating the redirect, it will likely cause an error.


*It's possible, though not likely, that includes will work differently on your localhost server. However, any relative HTML URL's on them will be subject to the same limitations/issues as other relative HTML URL's that make up the page's final served HTML content.