URL Rewriting

gillygilly Junior MemberShared Hoster
I need some assistance, im trying to rewrite my urls using .htaccess but nothings happening!

1) How do i check to see if mod_rewrite is installed?

2) Is there anything wrong with this, i have a news.php page and you can browse by news.php?page=2

RewriteEngine on
RewriteRule ^news/$ news.php
RewriteRule ^news/([0-9])$ news/$1/ [R]
RewriteRule ^news/([0-9])/$ news.php?page=$1

Help please!

Comments

  • NuvoNuvo Forum Leader VPS - Virtual Prince of the Server
    If you're using a directory structure so news/post would show the same as news.php?post=1, you need to remember that Apache will look for files in the /news/ directory when working with relative links.
    Since /news/ doesn't actually exist, it won't find the files it's looking for.
    The way most people get around this is to use the full path to the files rather than relative paths like images/image.png.
    I got around this by making it appear as if there was an actual file rather than a directory.
    I have news.html rather than /news/ which works great, but it does mean you end up with URL's like news_1234.html when using 2 variables in a query string.
    This might work:
    RewriteEngine On
    RewriteRule ^news_([0-9]).html$ news.php?page=$1 [L]
    
    Basically, it should make news.php?page=1 into news_1.html and so on.
    Remember to update your links or you'll still end up on news.php?page=1 and not the rewrite URL.

    You can try asking your host if they support mod_rewrite through their support forums or whatever they have.
    Most decent hosts will have the mod_rewrite Apache module available since some top end sites use it (devdreams uses it on the main site).
    PHP, CSS, XHTML, Delphi, Ruby on Rails & more.
    Current project: CMS Object.
    Most recent change: Theme support is up and running... So long as I use my theme resource loaders instead of that in the Rails plug-in.
    Release date: NEVER!!!
  • gillygilly Junior Member Shared Hoster
    Okay i've had it confirmed by my host and they do support mod_rewrite.

    But i cant get the example above working!
  • gillygilly Junior Member Shared Hoster
    Not sure if many people we're are reading this but i've managed to get my url_rewriting done, if anyone's interested in doing it for themselves his some code i wrote for mine.
    RewriteEngine on
    RewriteBase /
    
    RewriteRule ^news\.php$ news/
    RewriteRule ^news$ news/ [R]
    RewriteRule ^news/$ news.php
    
    
    RewriteRule ^portfolio$ portfolio/ [R]
    RewriteRule ^portfolio/$ portfolio.php
    
    RewriteRule ^portfolio/category/([a-zA-Z0-9]+)$ portfolio/category/$1/ [R]
    RewriteRule ^portfolio/category/([a-zA-Z0-9]+)/$ portfolio.php?cat=$
    
    Thats about 1/8 of my code, there probably was a shorter way but it still works!
Sign In or Register to comment.