Jump to content

Introducing Pretty URLs?


justlukeyou

Recommended Posts

Hi,

 

I have followed a guide on how to introduce pretty URLS however it has no impact on my links. My links still show as

 

website.com/products/product.php?=redwidget

 

Am I doing it correctly.  Do I need to change my links at all?

 

http://www.nouveller.com/quick-tips/quick-tip-6-how-to-write-clever-pretty-urls-with-htaccess/

 

I created text file and renamed it .htacess and copied and pasted in as per the guide

 

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([a-z]+-?[a-z]+)/$ /$1.php

Link to comment
Share on other sites

Thats entirely dependent on how you've defined your regex in htaccess.

 

In your case it appears as though you can have domain.com/this-is-my-url/ but I'm not convinced you can have anything after that. Moreover, it takes the this-is-my-url and expects it to be a PHP page.

 

Your better off redirecting everything to a handler page and either appending a query string or giving the re-written URL some structure.

 

E.g. I often use

 

RewriteRule ^([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?(.*)$ site.php?controller=$1&action=$2&queryString=$3

 

Where $1 corresponds to the first set of brackets and so on.

Link to comment
Share on other sites

E.g. I often use

 

RewriteRule ^([a-zA-Z0-9-]+)?/?([a-zA-Z0-9-]+)?/?(.*)$ site.php?controller=$1&action=$2&queryString=$3

 

I would rather have the application itself figure out where to route the traffic. I guess that would work alright on very simple websites, but it greatly limits when you can do.

Link to comment
Share on other sites

Thanks everyone, its becoming what I need to do.  I thought there was a simpler, standard way of introducing pretty URLS.

 

This is a typical link for my site .com/products/productscategorised.php?name=bed

 

How would a layout a link this, I have tried .com/products/productscategorised/bed

 

 

Link to comment
Share on other sites

I'm yet to find a limit and have built some extremely large sites using that method. I'm still struggling to grasp why that method isn't good for large sites, and yes it has been suggested before to me but never explained.

Link to comment
Share on other sites

I think it just limits further "prettification" of URL's, because you can't use routing.

 

For example say you want to display a news page. Let's say the URL is example.com/news. What if you introduce pagination? You would have to do something like example.com/news/page/1, where "news" is the controller and "page" is a method. You'll probably either have a hacky-looking workaround or code duplication too, since you are displaying essentially the same thing for the "page" method as you would the default method.

 

Using routing you can easily route example.com/news/page/1 or just example.com/news/1 to the default method.

 

That's just a quick example, but I use the routing features of CodeIgniter and FuelPHP all the time.

Link to comment
Share on other sites

I have added this to my htaccess file.

 

However when I use /products/roomproductscategorised.php/bedroom instead of /products/roomproductscategorised.php?room=bedroom it doesn't work.  However /products/roomproductscategorised.php?room=bedroom does work.

 

 

RewriteEngine On

RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

 

Im confused as to what rewrite code I use to make this work.

 

I thought there would be standard ways to introduce pretty URLs?

Link to comment
Share on other sites

I think it just limits further "prettification" of URL's, because you can't use routing.

 

For example say you want to display a news page. Let's say the URL is example.com/news. What if you introduce pagination? You would have to do something like example.com/news/page/1, where "news" is the controller and "page" is a method. You'll probably either have a hacky-looking workaround or code duplication too, since you are displaying essentially the same thing for the "page" method as you would the default method.

 

Using routing you can easily route example.com/news/page/1 or just example.com/news/1 to the default method.

 

That's just a quick example, but I use the routing features of CodeIgniter and FuelPHP all the time.

 

I've never considered using a URI router before. I would probably benefit massively creating a router and instead of having everything hard-coded for a controller/action format, just instantiate the controller and execute the action based on the command in the URI? That offers a far more dynamic approach to how the URIs can be constructed as I don't HAVE to follow the controller/action set-up, what do you think scootstah? That's the fundamental purpose of the router I guess, to provide dynamics.

Link to comment
Share on other sites

RewriteEngine On

RewriteRule ^([a-z]+)/([a-z\-]+)$ /$1/$2.php [L]

 

Here your saying:

 

(any lower case a-z) / (any lower case a-z or - (hyphen)) map into domain.com/firstValue/secondValue.php

 

Its not quite correct. Not sure what level of programming you are with PHP but you may want to look into routers, as scootstah has pointed out, if your going to get into pretty URIs as your current thinking would redirect to a single page; it is not dynamic.

 

If you were looking to do it for that specific page you want something like:

 

RewriteRule ^products/category/([a-zA-Z-]+)$ location/of/file/roomproductscategorised.php?room=$1

 

Allowing you to write http://www.domain.com/products/category/bedroom/. In reality though, this isn't particularly useful as its a combination of the server-side language combined with htaccess that gives a good system which leads us back to the whole routers and what not.

Link to comment
Share on other sites

Thanks,

 

I followed a guide for this.

 

Ive got is so /products/products.php works fine as /products/products (without the .php) however I cant get /products/roomproductscategorised.php?room=bedroom to work as /products/roomproductscategorised.php/bedroom

 

Any ideas or should I do it differently? There must be a simple way to do this.

Link to comment
Share on other sites

Thanks,

 

I tried this

 

RewriteRule ^products/category/([a-zA-Z-]+)$ /products/roomproductscategorised.php?room=$1

 

My links is

 

website.com/products/roomproductscategorised.php?room=bedroom

 

I also tried it like this but cant get it to work.

 

RewriteRule ^products/category/([a-zA-Z-]+)$ /products/roomproductscategorised.php?room=

 

Link to comment
Share on other sites

Your thinking very much in a straight line. X=X instead of X=(A to Z) kind of thing; pretty URLs are all about removing the query string, or hiding it, and allowing the user to type far more user-friendly URLs which lead to the same result.

 

URL = Uniform Resource Locator

URI = Uniform Resource Identifier

 

URI is a term used to describe both the URL and URN (Uniform Resource Name) as one. Everything on the web has a Name (URN) and Location (URL). Together, they can be described as the Uniform Resource Identifier.

 

So you can discuss roomproductscategorised.php (which may be considered a URN) found at www.domain.com/products/page.php?foo=bar (a URL). Together they form a URI. Its probably more complex than that but that's my understanding. You can go further with URCs which I think are to do with meta-data or something, not sure.

 

The above may be slightly off, however.

Link to comment
Share on other sites

Just reading up on it. A URN is another type of URI, its got very little to do with a URL. Go read up and you'll see what I mean. You just need to know a URL is a URI. URLs fall under URIs (Y)

Link to comment
Share on other sites

I've already told you how to do that... but what your asking won't then work for every other page... simple URLs aren't a case of wave your wand and boom. You've gotta understand whats happening and then how to handle it else you'll be sat there writing a new line for every single page in htaccess...

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.