Jump to content

.htaccess and easy URIs


deadonarrival

Recommended Posts

Hey guys

 

I had a perfect setup for this years ago but lost it in a hard drive crash (it wasn't part of a project, just a test). What I'm trying to do is the standard

http://website.com/seo/friendly/urls/ but can't quite get it right

 

I seem to remember that my system just redirected everything to index.php (invisibly) and PHP did some parsing of the URL to put values into arrays

So first up, I need some .htaccess to redirect everything to index.php

 

I have

#Rewrite on
RewriteEngine On

#If the URL points to a real file/directory, don't redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

#Send site.com/a/b/c/ to site.com/index.php
RewriteRule ^/?(.+)$ http://site.com/~user/sitename/index.php?values=$1 [L]

 

This works in terms of it gets the values to index.php but has problems:

1) I have to use a full URL (including http://site.com) because if I just put index.php?values=$1 or /index.php?values=$1 I get 404 errors - this means I can't just move straight over to the production server

2) It's not invisible - the URL bar no longer shows http://site.com/a/b/c/, it shows http://site.com/index.php?values=a/b/c/

 

So I think I'm doing something wrong, and I can't for the life of me remember how I did it last time. I know it's possible, because I definitely remember the end results being that I took it a stage further and had something like http://site.com/page/a.b/1.2/ mapping to index.php and including page.php within that, with $values[a] = 'b' and [1] = [2]

 

Any hints/tips/"look here"s/fully working code snippets/"you're an idiot, it's this"s are gratefully accepted

Link to comment
Share on other sites

Aha, got it. I found an old post on here documenting when I got it right first time round, back in 2008

 

http://forums.phpfreaks.com/index.php?topic=198062.msg894306#msg894306

 

First up I had to set the Rewrite Base

RewriteBase /path/to/site/

 

Then the rule I used was

RewriteRule ^/?(.+)$ index.php?values=$1 [L,QSA]

Which takes http://site.com/a/b/c/ and gives a $_GET['values'] variable which looks like a/b/c/ and can then be parsed by splitting.

 

PHPFreaks, helping me to help myself.

Link to comment
Share on other sites

Okay, I've mostly solved it...

 

With the above, the url cleanly redirects to index.php with the variables easily accessible. The obvious problem being that EVERYTHING gets redirected, and I need to ignore certain files.

 

I made a simple regex to ignore .png etc, but with the mod_rewrite then any image in the site with a relative path eg img/pic.png is being rewritten to http://site.com/a/b/c/img/pic.png rather than http://site.com/img/pic.png

 

I can solve this by adding / to the start of images, but this is a bit counter-intuitive as I'd have to remember to do it every time: that's fine for now while it's just me working on it, but in 6 months I'll forget that and be scratching my head again.

 

I've come up with the following which is "nearly there". It works if the url is site.com/ or site.com/a but not if it goes any further eg site.com/a/ or site.com/a/b. Can anybody spot where I'm going wrong?

 

^(/.*/)*(/.*\.(png|css|jpg|jpeg|gif|js)+)$

 

I think what I need to do is use regex to split it into three sections, but 2 is turning out to be tricky

1) /a/b/c/d/e/....ad infinitum

2) = /img/ subfolder (possibly optional, things can be in root)

3) = file.png

 

My other option is to write some PHP functions for images in order to remove anything after the base site path and write the URL there - ie get the location of /site/index.php, strip anything after site/ and add image.png after that. The problem here is as before, in 6 months I'll forget that I have to use the function.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.