A Flash Developer Resource Site

Page 1 of 2 12 LastLast
Results 1 to 20 of 27

Thread: [RESOLVED] URL rewriting does not work on localhost

  1. #1
    Member
    Join Date
    Jan 2007
    Posts
    79

    resolved [RESOLVED] URL rewriting does not work on localhost

    Hi all,
    I am building a CMS website in PHP on my localhost and I have dynamic URLs, something on the lines of:

    www.mysite.com/index.php?page1=1&page2=2

    Now I was trying to get the links to display in Search Engine Friendly format, preferably something on the lines of:
    www.mysite.com/index.html

    www.mysite.com/about.html
    www.mysite.com/contact.html

    I even tried other searches from Google but none of them helped. As I am doing this on my localhost on my PC, I created a .htaccess file and placed the clean url code into it and placed the .htaccess file in the site root i.e. in "mydomain" folder, which in turn is located in "wamp" folder.

    Hopefully someone would be able to help me through this as I have been seaching for an answer for a long time. Any help is highly appreciated.

    Thank you in advance.

  2. #2
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Does it work when it's not running locally?

    Are you getting a 500 error?

    What's it actually doing/not doing? What's your code? You're leaving out lots of information!

  3. #3
    Member
    Join Date
    Jan 2007
    Posts
    79

    Cool

    Hi,
    Thanks for the reply. Here's the contents of my .htaccess file:

    PHP Code:
    Options +Indexes
    Options         
    +FollowSymLinks
    RewriteEngine   On
    RewriteBase     
    /
    RewriteCond     %{REQUEST_FILENAME}     !-f
    RewriteCond     
    %{REQUEST_FILENAME}     !-d
    RewriteRule     
    ^([a-zA-Z0-9\-]+)/?$    /index.php?pp_id=$[L,QSA]
    RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$   /index.php?pp_id=$1&page_id=$[L,QSA]
    #RewriteRule     ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$    /index.php?category=$1&page=$2&section=$3 [L,QSA] 
    The line: LoadModule rewrite_module modules/mod_rewrite.so
    is already uncommented in httpd.conf file.

    It earlier gave me 500 error when I was experimenting with the previous code that I had in the .htaccess file. But with the code that I have indicated above, it does not show me the 500 error.

    It still does not work. Can you please help me with this?

    Thank you.

  4. #4
    Member
    Join Date
    Jan 2007
    Posts
    79
    What it is doing:
    It is displaying the URLs in the following format:
    www.mysite.com/index.php?page1=1&page2=2

    What it is not doing:
    Say I am able to access "About Us" content at www.mysite.com/index.php?page1=2, I want it to display the URL as www.mysite.com/about.html
    But it's not doing that.

    Hope that gives some more info on what I was trying to do.

    Thank you.

  5. #5
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Your rewrite rules are looking for something completely different than you're asking for.

    It's going to rewrite a request like: mysite.com/about/
    to: mysite.com/index.php?pp_id=about

    There is no way you're going to be able to translate "about" to a number unless you hardcode every possible combination like: RewriteRule ^about\.html$ /index.php?page1=2 [L]

    Does that help you?

  6. #6
    Member
    Join Date
    Jan 2007
    Posts
    79
    Hi there,
    Thanks much for the response. I tried inserting your line:
    RewriteRule ^about\.html$ /index.php?page1=2 [L]
    and restarted my localhost server on my PC and tried to browse the website with the hope of finding the about.html attachment when page1 = 2, but it does not show up. It still shows up as index.php?page1=2

    If about.html cannot be obtained, I am willing to settle down to rewrite a request like: mysite.com/about/

    I think it will help if we can at least get to that stage but it does not work. My host does not provide me access to .htaccess file online. So can we do something about this on my localhost please?

    Thank you.

  7. #7
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    This should be your entire .htaccess file:
    Code:
    RewriteEngine on
    RewriteRule ^about\.html$ /index.php?page1=2 [L]
    Then in your browser you should enter in http://localhost/about.html Internally, that should rewrite the url to /index.php?page1=2

    If that doesn't work, you don't have .htaccess overrides working.

  8. #8
    Member
    Join Date
    Jan 2007
    Posts
    79
    Hi,
    Thanks for your response. I tried what you said. Just placed those 2 lines in my .htaccess file, restarted my server and tried to access the url at:
    http://localhost/mydomain/about.html

    Instead of navigating to http://localhost/mydomain/index.php?page1=2, it navigates me to http://localhost/

    As per your response, I understand that this might be a problem .htaccess overrides. If that's the case, how do I go about tackling this one?

    Please let me know. Thanks much.

  9. #9
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Now you're introducing more directories into the mix. My rewrite rule works on localhost/about.html only.

    Where is your .htaccess file sitting? Inside the mydomain directory? Or at the top level?

  10. #10
    Member
    Join Date
    Jan 2007
    Posts
    79
    Hi,
    Sorry if I the information I left out was crucial. Let me brief up again. I have PHP installed on my pc and I have my website which can be accessed at:
    http://localhost/mydomain/

    The .htaccess file is located in mydomain folder. From what I gathered, the .htaccess file must be placed in the root directory of the website when it is being hosted online and must be in the main directory when the website is being developed. As I am developing it locally on my PC, I have placed the .htaccess file in mydomain folder. During my previous experiments with .htaccess file, I received 500 Internal Error couple of times. At that time, I had the .htaccess file located in mydomain folder. As my changes in .htaccess were reflecting the website's functionality, I figured out that mydomain folder was the right place to have the .htaccess file and hence I proceeded with that and still have it in the mydomain folder.

    Hopefully I have included the information you needed me to address. Look forward for your reply.

    Thanks again.

  11. #11
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    If your .htaccess file is sitting in /mydomain, it should read:
    Code:
    RewriteEngine on
    RewriteRule ^about\.html$ /mydomain/index.php?page1=2 [L]
    If that's not working, you have something bizarre going on. That's about as basic of a rewrite as you can get.

    [edit]For your reassurance, I just set this up identically on my localhost, and it worked exactly. If I went to: http://localhost/mydomain/about.html, it was rewriting to http://localhost/mydomain/index.php?page1=2[/edit]
    Last edited by MyFriendIsATaco; 05-31-2009 at 08:59 PM.

  12. #12
    Member
    Join Date
    Jan 2007
    Posts
    79

    Thumbs up

    Cheers to you, my friend! I got your new code in the .htaccess file and when I visited http://localhost/mydomain/about.html, I was able to access the index.php?page1=2 content. Got it working so far. Thanks to you!

    I also realized that I would need to do the same for every page generated. But since this is for a CMS, the pages are subject to deletion, which would mean that if I hardcode about.html to be accessed at page1=2 and if it's deleted and recreated in the future, our code in .htaccess file would not work. Also if hundreds of pages are created, then every value must be hardcoded which becomes cumbersome.

    So is there a way to tackle this situation as the Page IDs are dynamic? I even have menus, which on rollover, show in status bar that the links point to:
    http://localhost/mydomain/index.php?page1=2,
    http://localhost/mydomain/index.php?page1=3,
    and so on...

    Is there any way, we can make the menus show up as
    http://localhost/mydomain/about.html
    http://localhost/mydomain/contact.html
    and so on...?

    Appreciate all your help. Thanks much.

  13. #13
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Yep. That's where a thing called a "slug" comes into play. It's basically a string ID for your page. So it's just another object that has to be added to the database.

    For example, someone creates a new page for Contact, they'd want to assign a slug of "contact".

    Then your .htaccess would look like this:
    Code:
    RewriteEngine on
    RewriteRule ^([0-9a-zA-Z_-]+)\.html$ /mydomain/index.php?page_slug=$1 [L]
    See what I did there? It's going to capture the actual page name they typed in. Then it's going to pass that value to a page_slug variable. You can then query the database based on that slug instead of the number values.

    So a hit on a url like: /mydomain/contact.html would translate to /mydomain/index.php?page_slug=contact

  14. #14
    Member
    Join Date
    Jan 2007
    Posts
    79
    Hi bud,
    Sorry, I thought I replied back to you but I am bit surprised and bit shocked that I did not find my reply. Do you know if it was deleted for some reason?

    Anyways, I would like to thank you for the above solution. It did work for me. I created a new column called slug and pulled the data using that. So that did the trick as you mentioned. Apart from that, I tried to follow the same trick for sub-directories within the root folder of the website, but it did not work. Here's an example:

    http://localhost/mydomain/members/contact.html

    When I visit the link above, it says "Page not found". Here's the modified version to accommodate the sub-directory as well:

    RewriteEngine on
    RewriteRule ^([0-9a-zA-Z_-]+)\.html$ /mydomain/index.php?page_slug=$1 [L]
    RewriteRule ^([0-9a-zA-Z_-]+)\.html$ /mydomain/members/index.php?page_slug=$1 [L]

    Although I did this, it did not work. Any clues on how to make it work when I am using sub-directories?

    Appreciate all your help and thanks a ton.

  15. #15
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Which directory is your .htaccess file sitting in?

  16. #16
    Member
    Join Date
    Jan 2007
    Posts
    79
    It's in the main directory (root folder of the website). In my case, its located in "mydomain" folder. Link for localhost testing:
    http://localhost/mydomain/members/contact.html

    Hope that explains. Thanks.

  17. #17
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    Then your rewrite rules need to include that /members/ directory.

    Try this out:
    Code:
    RewriteRule ^members/([0-9a-zA-Z_-]+)\.html$ /mydomain/members/index.php?page_slug=$1 [L]

  18. #18
    Member
    Join Date
    Jan 2007
    Posts
    79
    Excellent. It did the trick. I am gonna mark the thread as solved. Thanks a ton!

    This may be slightly off-topic, but while I was working with the rewrite, the thought of URL rewriting without using .htaccess occurred to me. I may not be able to get access to .htaccess with the website that I host with, so I was thinking if there was an alternate way to do this.

    Please let me know when you get a chance. Thanks much.

  19. #19
    Bearded (M|G)od MyFriendIsATaco's Avatar
    Join Date
    Dec 2002
    Location
    Awesomeville.
    Posts
    3,045
    There is no way to have a fully CLEAN url without .htaccess.

    What most systems do is handle the rewrite rules inside their scripts though. Wordpress is a good example.

    What happens is a URL is constructed as such: http://mysite.com/index.php/members/matt

    Now, inside index.php, the "/members/matt" part is accessible via the variable: $_SERVER['PATH_INFO']

    So you could then parse that string with a regular expression internally and do whatever you want with it.

    If you did then want to "turn on" clean urls, you'd only need a simple rule in an .htaccess file:
    Code:
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*) /index.php/$1 [L]

  20. #20
    Member
    Join Date
    Jan 2007
    Posts
    79
    The "/members/matt" approach looks clean to me (may not be the cleanest), but will do the trick in some cases. Will I still need access to .htaccess file?

Tags for this Thread

Posting Permissions

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




Click Here to Expand Forum to Full Width

HTML5 Development Center