.htaccess dilemma

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raamay
    New Member
    • Feb 2007
    • 107

    .htaccess dilemma

    experts, i am in a mood to create clean URLs for my site. In this regard i am trying to use .htaccess. But i am absolutely nill with it. I tried a hell lot of tutorials to learn it but it was of no use. I only know how to set the DirectoryIndex so far.

    Well, i am using WAMP as my webserver in my machine and my files are not yet ready for upload. My files reside in C:\wamp\www\mys ite folder. I have an index page as index.php in my root directory. I access my index page as http://localhost/mysite/index.php. Now i have a page in the root directory as class.php requiring a dynamic parameter something like http://localhost/mysite/class.php?i=2323. Now how can i make it into something like this http://localhost/mysite/class.php/2323

    Please give me a clear direction step by step.
  • Markus
    Recognized Expert Expert
    • Jun 2007
    • 6092

    #2
    Originally posted by raamay
    experts, i am in a mood to create clean URLs for my site. In this regard i am trying to use .htaccess. But i am absolutely nill with it. I tried a hell lot of tutorials to learn it but it was of no use. I only know how to set the DirectoryIndex so far.

    Well, i am using WAMP as my webserver in my machine and my files are not yet ready for upload. My files reside in C:\wamp\www\mys ite folder. I have an index page as index.php in my root directory. I access my index page as http://localhost/mysite/index.php. Now hereafter what should i do?

    A basic and clear tutorials would be a great help.
    The question is: how would you like your URL to end up? You have to have some idea.

    Anyway, the general use of this it to read the url htxp://example.com/dir1/articles/2 as htxp://example.com/dir1/index.php?categ ory=articles&id =2. Which is, I suspect, somewhat like you're after?

    Anyway, I'll dive into it.

    First, we turn the engine on.
    Code:
    RewriteEngine On
    If you're working in a directory other than root, it is best to set the base dir. I'm not sure if this is necessary, because I remember not having to do this a while ago when running XAMPP, but since switching to EasyPHP, I've had to use it. So, set this to the dir you are currently working in. I am assuming you have the .htaccess file in the directory you want to work with. If you are rewriting the the root directory, you'll have to make according changes to the paths.
    Code:
    RewriteEngine On
    # The .htaccess file is in /root/dir1/
    RewriteBase /dir1/
    Now we want the rule.
    Code:
    RewriteEngine On
    RewriteBase /dir1/
    RewriteRule ^([a-zA-Z_-]+)/([0-9]+)$ index.php?cat=$1&id=$2 [R]
    ^ Starts the rule. The regular expression (rule) basically looks for any alpha character, underscore and hyphen (a-zA-Z_-), and also allows for more than one occurence (+). The value found is then stored in a variable for later use ($1)*. Then we check for a forward slash. After that, we look for any numeric character, allowing multiple occurences. Again, this is stored in a variable for later use ($2). $ Ends the rule.

    We next specify the url it should be interpreted as, using the stored variables. After this comes the flag [R] - this forces the URL to be rewritten, meaning the the URL will change. If you want the url to remain masked, you can omit this flag.

    * Variables are stored successively, 1, 2, 3 ...

    This should get you started.

    If you have any questions, let us know.

    - Markus.

    Comment

    • raamay
      New Member
      • Feb 2007
      • 107

      #3
      ok my .htaccess looks like this
      # this is the initialization
      Options +FollowSymLinks
      RewriteEngine On
      RewriteBase /
      # these are the rewrite conditions
      RewriteCond %{REQUEST_FILEN AME} !-f
      RewriteCond %{REQUEST_FILEN AME} !-d
      # and finally, the rewrite rules
      RewriteRule ^([a-zA-Z0-9\-]+)/?$ class.php?page= $1 [L,QSA]
      RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ class.php?page= $1 [L,QSA]
      RewriteRule ^([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/([a-zA-Z0-9\-]+)/?$ class.php?page= $1 [L,QSA]
      Now how should i link this page in my site so that i get the desired way. I mean in this form http://localhost/mysite/class.php/1. And another thing is that if i directly type and access the page above, the page is displayed but i get just the skeleton page with no css and other graphics working. And if i click any of the links in this page, i am directed to pages like http://localhost/mysite/class.php/anotherpage.php. Please help me out!

      Comment

      Working...