Connect with Expertise | Find Experts, Get Answers, Share Insights

Getting PHP to read the clean URL created by .htaccess

 
Join Date: Mar 2010
Posts: 1
#1: Mar 9 '10
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

dlite922's Avatar
E
C
 
Join Date: Dec 2007
Location: Denver, CO
Posts: 1,307
#2: Mar 9 '10

re: Getting PHP to read the clean URL created by .htaccess


You need to make an exception for js and css files I think.

Not sure.
 
Join Date: Mar 2010
Posts: 49
#3: Mar 9 '10

re: Getting PHP to read the clean URL created by .htaccess


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
Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#4: Mar 10 '10

re: Getting PHP to read the clean URL created by .htaccess


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.
Atli's Avatar
E
M
C
 
Join Date: Nov 2006
Location: Iceland
Posts: 4,618
#5: Mar 10 '10

re: Getting PHP to read the clean URL created by .htaccess


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
Reply

Tags
htaccess, php