473,242 Members | 3,767 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,242 software developers and data experts.

.htaccess dilemma

107 100+
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\mysite 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.
Mar 14 '09 #1
2 1355
Markus
6,050 Expert 4TB
@raamay
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?category=articles&id=2. Which is, I suspect, somewhat like you're after?

Anyway, I'll dive into it.

First, we turn the engine on.
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine On
  2.  
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.
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine On
  2. # The .htaccess file is in /root/dir1/
  3. RewriteBase /dir1/
  4.  
Now we want the rule.
Expand|Select|Wrap|Line Numbers
  1. RewriteEngine On
  2. RewriteBase /dir1/
  3. RewriteRule ^([a-zA-Z_-]+)/([0-9]+)$ index.php?cat=$1&id=$2 [R]
  4.  
^ 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.
Mar 14 '09 #2
raamay
107 100+
ok my .htaccess looks like this
# this is the initialization
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
# these are the rewrite conditions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-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!
Mar 17 '09 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: yawnmoth | last post by:
i'm trying to write a php script that will password protect some random directory by creating a .htaccess file, and a password file to accompany the .htaccess file, and it isn't working... ...
8
by: Joshua Beall | last post by:
Hi All, How do I disable magic quotes via .htaccess? I put the following file in my webroot, but it does not disable magic_quotes_gpc (according to phpinfo(), both the local and master value...
4
by: Ivo | last post by:
Greetings newsgroup, I am moving some php scripts to a new host. While getting to know the server, I ran into a strange problem. If I add a .htaccess file, or more specifically: a .htaccess with...
7
by: John | last post by:
Hello. I want to get this blasted .htaccess file sorted out, so I can have sessions without register_globals being on. I have looked everywhere for info on this and I mean everywhere...
0
by: Stoco | last post by:
I am trying to create a generic interface that will manage various ..htaccess protected directories. In the ideal world, the .htaccess would trigger a cgi script that would take login information...
4
by: Bill | last post by:
Does ASP have a file whose function is similar to .htaccess on Apache? I'm looking to protect subdirectory content.
0
by: Jack Hambabo | last post by:
Hi, I'm searching for a php script that can find out whether the current user (I know _SERVER will give me the name for non-cgi php) has the right to view a specific file. My dream is that I...
1
by: nickyeng | last post by:
I have checked this info from apache website, and i confused with it. Procteing System files it said: To run a really tight ship, you'll want to stop users from setting up .htaccess files...
0
by: asherwolf | last post by:
Hi, I'm trying to do something I think is pretty neat, but I've just about pulled my hair out by the behavior of my server. I'm hosting on GoDaddy, using a subdomain (www.mywebpage.com maps to...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
1
by: davi5007 | last post by:
Hi, Basically, I am trying to automate a field named TraceabilityNo into a web page from an access form. I've got the serial held in the variable strSearchString. How can I get this into the...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.