473,320 Members | 1,612 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,320 software developers and data experts.

Getting PHP to read the clean URL created by .htaccess

1
Hi there, I wonder if you can help me out.

I'm trying to create clean URLs and so far I've managed to use .htaccess to convert the following URL:

http://www.example.com/index.php?cat...on1&page=Page1

into:

www.example.com/Section1/Page1/

The problem is then getting PHP to interpret the URL and extract the variables. Here's the code I'm using:

.htaccess code:
Expand|Select|Wrap|Line Numbers
  1. Options +FollowSymLinks
  2. Options +Indexes
  3. RewriteEngine on
  4.  
  5. #INTERNAL STATIC URL TO DYNAMIC URL
  6. RewriteRule ^/([^/]+)/?$ /index.php?c=$1 [L]
  7. RewriteRule ^/([^/]+)/([^/]+)/?$ /index.php?c=$1&p=$2 [L]
  8.  
  9. #EXTERNAL DYNAMIC URL TO STATIC URL
  10. RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagory=([^&]+)\ HTTP/
  11. RewriteRule ^index\.php$ http://www.example.com/%1/? [R=301,L] 
  12. RewriteCond %{THE_REQUEST} ^[a-zA-Z0-9]{1,3}\ /index\.php\?catagroy=([^&]+)&page=([^&]+)\ HTTP/
  13. RewriteRule ^index\.php$ http://www.example.com/%1/%2/? [R=301,L]
PHP code:
Expand|Select|Wrap|Line Numbers
  1. $urlVariables = explode("/",$_SERVER['REQUEST_URI']);
  2. $catagory = $urlVariables[1];
  3. $page = $urlVariables[2];
With this I keep getting a 404 error page. If I add index.php into this line of the htacces code:
Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^index\.php$ http://www.example.com/index.php/%1/? [R=301,L]
The page at least loads, but the css and images are not loaded.

Any suggestions to what I'm missing here?
Many thanks
Mar 9 '10 #1
4 5700
dlite922
1,584 Expert 1GB
You need to make an exception for js and css files I think.

Not sure.
Mar 9 '10 #2
Someone correct me if I'm wrong but I think that could be due to paths. Your no longer at a relative path you need to use absolute paths if your using rewrite.


/home/stylesheets/main.css != /home/rewriten/stylesheets/main.css
Mar 9 '10 #3
Atli
5,058 Expert 4TB
Hey.

To fix the stuff like images and css files, you could try something like:
Expand|Select|Wrap|Line Numbers
  1. RewriteCond %{REQUEST_URI} \.(css|js|jpe?g|png|gif|ttf|eot)$ [NC]
  2. RewriteRule ([^/\.]+)\.(css|js|jpe?g|png|gif|ttf|eot)$ /resources/$2/$1.$2
That should rewrite all of the listed extensions into "/resources/ext/file.ext".
(Note I haven't tested it yet.)

It is of course best to just use absolute paths when loading resources.
Mar 10 '10 #4
Atli
5,058 Expert 4TB
As to your internal rewrites, I see one potentially fatal problem.
Your first rule, "^/([^/]+)/?$", matches all requests that start with a "/", that include an undetermined amount of non "/" characters, and that may or may not end in "/"... That'll match pretty much everything, including "/index.php?c=xxx&p=yyy".

This will most likely leave you in an indefinite loop, eventually causing Apache to break out with a 500 code. (A match causes an internal redirect, which is then process again as a new request and subsequently passed through mod_rewrite again.)

Also, I'm not entirly sure you should be including the "/" char at the beginning of the regular expression. Apache may strip it from the request URL. (Not 100% sure though.)

Try something like this instead:
Expand|Select|Wrap|Line Numbers
  1. RewriteRule ^([^/]+)/?([^/]+)?/? index.php?c=$1&p=$2 [L,QSA]
(Note, this one replaced both of your internal requests. The second option is made optional by the ? char.)

P.S.
If you need to specify some specific directory before the "index.php", you may want to try the RewriteBase instead.
Expand|Select|Wrap|Line Numbers
  1. RewriteBase http://example.com/some/dir/
  2. RewriteRule ^([^/]+)/?([^/]+)?/? index.php?c=$1&p=$2 [L,QSA]
There the index.php in the RewriteRule is rewritten as http://example.com/some/dir/index.php?c=xxx&p=yyy
Mar 10 '10 #5

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

Similar topics

0
by: OutsiderJustice | last post by:
Hi, Windows XP has the builtin support for other languages, for example, Chinese, from which I can create a file with the name directly encoded in Chinese, and it's not the unicode (unreadable...
4
by: psk79 | last post by:
Hi all, I created a set of pages with PHP for login using a database. Now, I need to link to a lot of CGI programs in a folder with protected-access (.htaccess). The username and password are...
4
by: Jon Andersson | last post by:
Hi need some help to read a csv or xml file using js. I want to create the csv/xml file by using asp and passing some parameters to it while calling it from my javascript function.
1
by: René Paschold | last post by:
Hello, i write a xml file with following code: Dim xWriter As New XmlTextWriter(SaveFileContent.FileName, _ Encoding.UTF8) With xWriter .Formatting = Formatting.Indented
4
by: Stefan Bellon | last post by:
Hi all! I want to parse the contents of an .htaccess file from within PHP. The contents of the .htaccess file looks like this: <Files foobar.tar.gz> AuthType Basic AuthUserFile /foobar...
0
by: csgraham74 | last post by:
Hi guys, I am writing an application in asp.net but im attempting to write some client side validation. I have most of this working but i am having trouble reading cookies set in asp.net using...
2
by: brigitte | last post by:
I have installed the front end for my database on several machines (the front end is an mde), which links to a back end on a network drive all my users have access to. To prevent mapping errors,...
3
by: d3vkit | last post by:
I've been using apache and php to create clean urls for a while, and suddenly the other day it just stopped working. My host is godaddy.com and I think this might be the source of my trouble, but...
4
by: adnanjunk | last post by:
Hi, Having a little issue with .htaccess not working on windows server. I have clean url's setup using the htaccess file, working fine on linux server. I have researched for a while and found...
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...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
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...

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.