Results 1 to 6 of 6

Thread: Url Rewrite working as directory

  1. #1
    Join Date
    Jun 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Url Rewrite working as directory

    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:

    Code:
    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
    Last edited by jscheuer1; 06-08-2017 at 02:03 PM. Reason: format code

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    Have you tried:

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

    Code:
    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/
    Last edited by jscheuer1; 06-08-2017 at 03:07 PM. Reason: tried it
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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 -
    Code:
    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 - Click image for larger version. 

Name:	testnds.jpg 
Views:	67 
Size:	17.0 KB 
ID:	6180
    Last edited by jscheuer1; 06-09-2017 at 11:58 AM. Reason: format

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    That would seem to be a problem with the PHP code. I made up a test page:

    category.php:

    PHP Code:
    <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/en...te-module.html
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  5. #5
    Join Date
    Jun 2017
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default

    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

  6. #6
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    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:

    Code:
    <script src="encode.js"></script>
    Well, the server will be looking for that in the non-existing category folder. But if you go like:

    Code:
    <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):

    Code:
    <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:

    Code:
    <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.
    Last edited by jscheuer1; 06-13-2017 at 02:57 AM. Reason: add info
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

Similar Threads

  1. URL rewrite
    By crobinson42 in forum HTML
    Replies: 3
    Last Post: 01-09-2012, 12:04 AM
  2. Replies: 4
    Last Post: 06-04-2010, 03:24 AM
  3. Rewrite URL's
    By Valor Studios in forum Other
    Replies: 5
    Last Post: 07-18-2008, 01:53 AM
  4. MOD Rewrite
    By nikomou in forum Other
    Replies: 3
    Last Post: 05-08-2007, 09:28 AM
  5. mod rewrite (PHP URL)
    By Maximus in forum PHP
    Replies: 10
    Last Post: 01-13-2007, 11:09 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •