justlukeyou Posted June 7, 2012 Share Posted June 7, 2012 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 7, 2012 Share Posted June 7, 2012 Do I need to change my links at all? Of course you need to change your links. How else do you expect it them to change? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 7, 2012 Author Share Posted June 7, 2012 Thanks, what do I change them to? I've only written a small number of query links, I wanted to build the site first. Does website.com/products/product.php?=redwidget become website.com/products/product/redwidget Quote Link to comment Share on other sites More sharing options...
cpd Posted June 7, 2012 Share Posted June 7, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 7, 2012 Share Posted June 7, 2012 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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 8, 2012 Author Share Posted June 8, 2012 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 Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 https://www.google.com/search?q=php+pretty+urls&ie=utf-8&oe=utf-8&aq=t Quote Link to comment Share on other sites More sharing options...
cpd Posted June 8, 2012 Share Posted June 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 8, 2012 Share Posted June 8, 2012 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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 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? Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 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= Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 Okay I don't think you understand what's actually happening. what URI are you attempting to send to the web server? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 URI? Pretty Urls must be a common practice. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 9, 2012 Share Posted June 9, 2012 https://www.google.com/search?q=php+pretty+urls&ie=utf-8&oe=utf-8&aq=t Did you read any of these? Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 Yes I found out how to remove ".php" but I can't see how to run queries. Quote Link to comment Share on other sites More sharing options...
scootstah Posted June 9, 2012 Share Posted June 9, 2012 Pretty URL's remove the need to use querystrings. Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 Oh right, so how would I display: .com/products/roomproductscategorised.php?room=bedroom I thought it would be .com/products/roomproductscategorised/bedroom Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 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. Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 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) Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 Does anyone know how to turn .com/products/productpage.php?=bedroom into .com/products/productpage/bedroom Quote Link to comment Share on other sites More sharing options...
cpd Posted June 9, 2012 Share Posted June 9, 2012 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... Quote Link to comment Share on other sites More sharing options...
justlukeyou Posted June 9, 2012 Author Share Posted June 9, 2012 I'm sorry but I just don't understand what I need to do. What steps would you take to get this result? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.