473,378 Members | 1,639 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,378 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 1359
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: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...

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.