Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

apache mod_rewrite

Options
  • 12-03-2008 12:18pm
    #1
    Closed Accounts Posts: 658 ✭✭✭


    Hi

    I'm in a bit of a bind and I'm quite new to apaches mod_rewrite. I seem to be getting the hang of it apart from one thing. Paths! I'll explain below.

    I develop on my local machine some websites and I upload them to the live server. I have managed to get my rewrite rules working ok on my local machine but when I upload them to the live server, they don't work. I keep getting 404 errors. I'll explain my setup in more detail.

    My machine has apache 2.2 on mac osx 10.5, and I have the whole MAMP setup working swimmingly (Mac osx, Apache 2.2, Mysql 5, PHP 5). From looking at the various tutorials and examples on the web, I have configured my httpd.conf file and users httpd.conf file that mac users need to have setup.

    I am developing many sites so my webroot is /var/www/htdocs/ and in that I have the different directories for each site, so a site, lets say it's called 'ecommercesite' is located in /var/www/htdocs/ecommercesite/.

    At the top of each page in ecommercesite I have the base tag <base href="http://localhost/ecommercesite&quot; /> so that images and stylesheets will load irregardless of the depth of folders the user navigates into.

    A particular rewrite rule allows users to navigate product categories in a more url friendly way. An example is below:

    RewriteBase /

    RewriteRule ^?/products/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)?/$ products.php?main_category_name=$1&sub_category_name=$2 [L]

    You can imagine that because the user navigates two more paths deep in the url, that the images (because their src attribures are relavite) and stylesheets (same again) are relative, none of them will display. I use the base href tag to correct that. This also means the links will point to the correct location.

    This works ok on my local machine, but changing the base href to http://www.ecommercesite.tld and uploading it to live means that none of the links work. They all return a 404 error.

    Does anyone know is there any way to write these rules so that they are portable and I dont have to change things once I go live. Should I be using the base href tag at all or is there a better way of doing it ?

    Thanks for your replies :) Any help would be well appreciated.


Comments

  • Registered Users Posts: 2,569 ✭✭✭harringtonp


    Hi,

    Had a quick look, would guess that instead of

    RewriteRule ^?/products/([a-zA-Z0-9\-])/([a-zA-Z0-9\-])?/$ products.php?main_category_name=$1&sub_category_name=$2 [L]

    you may have wanted...

    RewriteRule ^?/products/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)?/$ products.php?main_category_name=$1&sub_category_name=$2 [L]

    as [a-zA-Z0-9\-] just matches a single character,+ means 1 or more of whatever precedes it, * means 0 or more and ? means 0 or 1

    Regards Paul.
    pontovic wrote: »
    Hi

    I'm in a bit of a bind and I'm quite new to apaches mod_rewrite. I seem to be getting the hang of it apart from one thing. Paths! I'll explain below.

    I develop on my local machine some websites and I upload them to the live server. I have managed to get my rewrite rules working ok on my local machine but when I upload them to the live server, they don't work. I keep getting 404 errors. I'll explain my setup in more detail.

    My machine has apache 2.2 on mac osx 10.5, and I have the whole MAMP setup working swimmingly (Mac osx, Apache 2.2, Mysql 5, PHP 5). From looking at the various tutorials and examples on the web, I have configured my httpd.conf file and users httpd.conf file that mac users need to have setup.

    I am developing many sites so my webroot is /var/www/htdocs/ and in that I have the different directories for each site, so a site, lets say it's called 'ecommercesite' is located in /var/www/htdocs/ecommercesite/.

    At the top of each page in ecommercesite I have the base tag <base href="http://localhost/ecommercesite&quot; /> so that images and stylesheets will load irregardless of the depth of folders the user navigates into.

    A particular rewrite rule allows users to navigate product categories in a more url friendly way. An example is below:

    RewriteBase /

    RewriteRule ^?/products/([a-zA-Z0-9\-])/([a-zA-Z0-9\-])?/$ products.php?main_category_name=$1&sub_category_name=$2 [L]

    You can imagine that because the user navigates two more paths deep in the url, that the images (because their src attribures are relavite) and stylesheets (same again) are relative, none of them will display. I use the base href tag to correct that. This also means the links will point to the correct location.

    This works ok on my local machine, but changing the base href to http://www.ecommercesite.tld and uploading it to live means that none of the links work. They all return a 404 error.

    Does anyone know is there any way to write these rules so that they are portable and I dont have to change things once I go live. Should I be using the base href tag at all or is there a better way of doing it ?

    Thanks for your replies :) Any help would be well appreciated.


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    Hi

    Thanks for the reply! Yep I have them pluses in, i just forgot to include them here. Assume that I have the pluses. I'm not at the computer I work with so I had no way of looking at the .htaccess file, so I just recited the rules from memory.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    change this

    ^?/

    to just ^products.........

    and make sure you have this at the very top of you .htaccess file:
    Options +FollowSymlinks
    RewriteEngine on
    RewriteBase /
    


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    Thanks man I'll give that a go tonight.


  • Closed Accounts Posts: 658 ✭✭✭pontovic


    Yep, that worked quite well. I set the base href at the top of each page to http://www.mylivesite.tld and altered the .htaccess rewrite rules as louie said and it worked. Thanks for the help ! :)


  • Advertisement
  • Closed Accounts Posts: 1,200 ✭✭✭louie


    No problem. Glad I could help.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    Iv problems with mod_rewrite on some servers. If I go to an incorrect URL, I got get a 404 but instead a 500 Internal Server Error. Its something a lot of people report but nobody seems to have a fix.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    How can you get to the incorect url in the first place.
    Obviously the rules you have are not properly formated and it breaks the code.

    Even the order of the rules matter in this case.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    louie wrote: »
    How can you get to the incorect url in the first place.
    Obviously the rules you have are not properly formated and it breaks the code.

    Even the order of the rules matter in this case.

    Iv no access to the files at the moment but ill show you it when I return to a PC that gives me better access.

    In the sense if someone goes to http://www.myplaces.com/acommodation instead of /accommodation it gives an internal error rather then a 404 - if you get me. :)


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    something is wrong alright but without seeing the .htaccess file can say what.

    add this to the file
    
    ErrorDocument 404 /index.php 
    ErrorDocument 401 /index.php
    ErrorDocument 400 /index.php
    ErrorDocument 403 /index.php
    ErrorDocument 500 /index.php
    
    you can create a 404.php and replace the index.php above - this way you can send yourself an email with the url that is trying to be accessed and do something about it.


  • Advertisement
  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    louie wrote: »
    something is wrong alright but without seeing the .htaccess file can say what.

    add this to the file
    
    ErrorDocument 404 /index.php 
    ErrorDocument 401 /index.php
    ErrorDocument 400 /index.php
    ErrorDocument 403 /index.php
    ErrorDocument 500 /index.php
    
    you can create a 404.php and replace the index.php above - this way you can send yourself an email with the url that is trying to be accessed and do something about it.

    Not a bad idea.. Its probably a nice touch to design error pages to match the site. Not sure if its all that common though.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    you can make use of the error pages and have a sitemap included into the page so users doesn't get frustrated when the big "OOOps" comes up.

    How many times you got to an 404. page and there was no link to navigate away from it? That's called bad design...


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    louie wrote: »
    you can make use of the error pages and have a sitemap included into the page so users doesn't get frustrated when the big "OOOps" comes up.

    How many times you got to an 404. page and there was no link to navigate away from it? That's called bad design...

    I just tested your site. Is it worth having such a fast redirect, if any at all? Its not going to stop people putting in the link each time if the error message disappears of the page so quick. They wont understand why until they can read the message.


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    When users arrives to a page like that, they don't read it but are actually scanning for a link back as the page itself (color and message) are self-explanatory....


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    louie wrote: »
    When users arrives to a page like that, they don't read it but are actually scanning for a link back as the page itself (color and message) are self-explanatory....

    Well I barley got a glimpse of the 404 on your site. It its me, I have a quick look at the title "404 Not Found" and have a fair idea whats a wrong. A link or redirect a few seconds later, fair enough but yours is pretty instant :P


  • Closed Accounts Posts: 1,200 ✭✭✭louie


    sorry Sully but what site is that?
    most of them gives 3 sec. before moving to the home page.
    I consider that long enough - but that's just my choice.


  • Moderators, Education Moderators, Technology & Internet Moderators, Regional South East Moderators Posts: 24,056 Mod ✭✭✭✭Sully


    louie wrote: »
    sorry Sully but what site is that?
    most of them gives 3 sec. before moving to the home page.
    I consider that long enough - but that's just my choice.

    That web design one. You barley get to read it before it flicks of.. Just my opinion though :) Btw, here is the contents of the .htaccess:-
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ $1.php [QSA,L]
    


Advertisement