SaranacLake Posted May 2, 2020 Share Posted May 2, 2020 A few questions about mod_rewrites... 1.) If I want to determine if the URL ends with either "list" or "gallery", and then take appropriate action, does the code below look correct? RewriteRule shop/(.+)/([list|gallery])$ shop/catalog.php?category=$1&view=2 [L] 2.) Is it bad to use mod_rewrites to determine how your website branches? For example... RewriteRule shop/music/(.*)$ go somewhere [L] RewriteRule shop/movies/(.*)$ go somewhere else [L] RewriteRule shop/books/(.*)$ go yet somewhere else [L] Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/ Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 5 hours ago, SaranacLake said: 1.) If I want to determine if the URL ends with either "list" or "gallery", and then take appropriate action, does the code below look correct? RewriteRule shop/(.+)/([list|gallery])$ shop/catalog.php?category=$1&view=2 [L] Try it and find out? Spoiler: no Hint: []s are for individual characters. 5 hours ago, SaranacLake said: 2.) Is it bad to use mod_rewrites to determine how your website branches? For example... RewriteRule shop/music/(.*)$ go somewhere [L] RewriteRule shop/movies/(.*)$ go somewhere else [L] RewriteRule shop/books/(.*)$ go yet somewhere else [L] Maybe? You'll have to be more specific than that. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577451 Share on other sites More sharing options...
gizmola Posted May 3, 2020 Share Posted May 3, 2020 The Rewrite looks fine to me. Oops, yeah should be (list|gallery) For aliasing I would suggest that rather than using rewrite, use mod_alias with the alias_match directive Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577452 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 (edited) 22 minutes ago, requinix said: Try it and find out? I'm just writing things out on paper to come up with a gameplan for my design, plus I don't have a way to test it right now. Quote Spoiler: no Hint: []s are for individual characters. Oh, right, I forgot. Haven't actively coded PHP or regexs in like 3-4 years. Quote Maybe? You'll have to be more specific than that. Okay, so I have been thinking about things while waiting for a response. Here is what I want to do. I am working on my e-commerce catalog and product details page. For the catalog I need a way to tell my PHP what kind of look to present, i.e. either a list of items OR a gallery of thumbnails of the items. And so I was going to have a URL like this... www.mystore.com/shop/movies/list/ www.mystore.com/shop/movies/gallery/ And for my product details page, I want a similar structure like this... www.mystore.com/shop/movies/the-irishman/ So I need some help from my Regex to help me sort out these similar looking URLs, and this is what I have for a solution so far... RewriteRule shop/(.+)/(list|gallery)$ shop/product-catgory.php?category=$1&view=$2 [L] RewriteRule shop/(.+)/(.+)$ shop/product-details.php?category=$1&item=$2 [L] So to my comment above, I am somewhat using my Regex to route which page things go to, because they have the same looking URL in either case, but but my Regex helps play traffic director. Thoughts? Edited May 3, 2020 by SaranacLake Fixed mistakes in what I said... Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577453 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 1 hour ago, SaranacLake said: Thoughts? What are you going to do if you need to support an item named List or Gallery? Don't mix concepts like this. It's okay to use query strings.There is absolutely nothing wrong with /shop/movies/?view=list. In fact there are some schools of thought that say the path portion indicates a resource and is not where you should be putting aspects of that resource's presentation. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577455 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 3 minutes ago, requinix said: What are you going to do if you need to support an item named List or Gallery? Don't mix concepts like this. Well, there will never be a product category called "list" or "gallery", so I am safe there. I am mixing things, because I don't have a way to take care of the presentation. Ideally I would use Javascript to handle the change in presentation, but I don't know Javascript, and that will have to be for a later version. 3 minutes ago, requinix said: It's okay to use query strings.There is absolutely nothing wrong with /shop/movies/?view=list. In fact there are some schools of thought that say the path portion indicates a resource and is not where you should be putting aspects of that resource's presentation. The reason I am mixing "organization" and "presentation" is that i wanted to have a nice looking URL, otherwise there is no need to use regex at all. Hmm... Looks like in this particular case, I am sorta stuck unless I did something like this... Product Catalog www.mystore.com/shop/movies/list/ www.mystore.com/shop/movies/gallery/ Product Details www.mystore.com/shop/movie-details/the-irishman/ I just wanted to keep the /shop/movies/<movie-title> format... 😕 Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577456 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 @requinix, Believe it or not, I don't use a smart phone. So, dumb question, but on a smart phone, do you even see the URL? And on a regular computer, does anyone even care about URL's anymore? (I've heard one person say that they don't matter anymore...) Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577457 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 14 hours ago, SaranacLake said: So, dumb question, but on a smart phone, do you even see the URL? Depends on the browser. I think it's common to show it when you're at the top of the page, then hide it as you scroll down. 14 hours ago, SaranacLake said: And on a regular computer, does anyone even care about URL's anymore? (I've heard one person say that they don't matter anymore...) No regular user has ever cared about URLs. For a while people thought making them look fancy was important for SEO, but it isn't. Your URLs should be something that a person can look at and not be afraid of. Like this page, it says "topic" (I am viewing a topic) and it has "mod_rewrite-questions" (that's the title). That's nice and reassuring. If it was just "/viewtopic.php?tid=310679" then that's not as nice or reassuring, but neither your web server nor Google actually care. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577491 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 (edited) @requinix, 1 hour ago, requinix said: No regular user has ever cared about URLs. For a while people thought making them look fancy was important for SEO, but it isn't. Your URLs should be something that a person can look at and not be afraid of. Like this page, it says "topic" (I am viewing a topic) and it has "mod_rewrite-questions" (that's the title). That's nice and reassuring. If it was just "/viewtopic.php?tid=310679" then that's not as nice or reassuring, but neither your web server nor Google actually care. So having a super pretty URL is something that the average user won't care much about, huh? If so, then maybe having everything look like this is a waste of effort? Product Catalog www.mystore.com/shop/movies/list/ www.mystore.com/shop/movies/gallery/ Product Details www.mystore.com/shop/movie-details/the-irishman/ Also, to your point above, is it the end of the world if I have a pretty URL with a query string on the end like.... www.mystore.com/shop/movies/?view=list www.mystore.com/shop/movies/?view=gallery Edited May 3, 2020 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577496 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 Yeah, it's fine. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577497 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 Just now, requinix said: Yeah, it's fine. So your only problem with the URLs in my OP is that I was mixing concepts into the 3 slots I had, right? Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577498 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 2 minutes ago, SaranacLake said: So your only problem with the URLs in my OP is that I was mixing concepts into the 3 slots I had, right? Yes. It's all much easier if you can set a pattern and use it for one specific purpose. /shop/thing browses a category, /shop/thing/thing shows an item in that category. You don't have to do any complicated logic about whether any part is a particular keyword and then have to change what the page does. Don't have to set up multiple URL rewriting rules to handle different exceptions to each pattern. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577499 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 4 minutes ago, requinix said: Yes. It's all much easier if you can set a pattern and use it for one specific purpose. /shop/thing browses a category, /shop/thing/thing shows an item in that category. You don't have to do any complicated logic about whether any part is a particular keyword and then have to change what the page does. Don't have to set up multiple URL rewriting rules to handle different exceptions to each pattern. So if I am going to do pretty URLs, then save them for where things are organized much like a filing system? And then leave things like sorting, filtering, product attributes for the query string? Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577501 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 3 minutes ago, SaranacLake said: So if I am going to do pretty URLs, then save them for where things are organized much like a filing system? And then leave things like sorting, filtering, product attributes for the query string? Right. The path portion identifies a particular thing. "Resource". Like the page that shows a category is a resource, and the page showing a particular product is a resource. If you want to change how that source is presented then stick it in the query string. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577502 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 (edited) 9 minutes ago, requinix said: Right. The path portion identifies a particular thing. "Resource". Like the page that shows a category is a resource, and the page showing a particular product is a resource. If you want to change how that source is presented then stick it in the query string. Okay, but i'm a little sure of the syntax to apply your suggestions. Pretty URL: /shop/movies/?view=listing Do I need an ampersand (&) here instead of a question mark (?) Ugly URL: /shop/product-catalog.php?category=movies&view=listing And not sure about the mod_rewrite... RewriteRule /shop/(.+)/________ /shop/product-catalog.php?category=$1&view=$2 [L] How do I get ?view=listing into variable $2?? Edited May 3, 2020 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577504 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 4 minutes ago, SaranacLake said: Do I need an ampersand (&) here instead of a question mark (?) Spend a minute reminding yourself about how URLs work. Quote How do I get ?view=listing into variable $2?? Learn about mod_rewrite's [QSA] flag. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577505 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 (edited) 22 minutes ago, requinix said: Spend a minute reminding yourself about how URLs work. Learn about mod_rewrite's [QSA] flag. How about this... Pretty URL: /shop/movies/?view=listing Quote RewriteRule /shop/(.+)/$ /shop/product-catalog.php?category=$1 [L, QSA] Edited May 3, 2020 by SaranacLake Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577507 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 Pretty good, but there's a couple things I should point out: 1. You require a trailing slash at the end of the path. You should make that optional (it's not significant to the meaning of the URL) and then enforce the canonical version (presumably with a slash) in code. 2. .+ will match as much as it possibly can. What will happen if the pattern tries to match against /shop/movies/and/more/ ? Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577508 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 14 minutes ago, requinix said: Pretty good, but there's a couple things I should point out: 1. You require a trailing slash at the end of the path. You should make that optional (it's not significant to the meaning of the URL) If the URL is always coming from a hyperlink then is it much concern? (I don't expect people to type in the URL, although they could.) Are you wanting something like this? RewriteRule /shop/(.+)(/)?$ /shop/product-catalog.php?category=$1 [L, QSA] 14 minutes ago, requinix said: and then enforce the canonical version (presumably with a slash) in code. Not sure I understand what you mean here... 14 minutes ago, requinix said: 2. .+ will match as much as it possibly can. What will happen if the pattern tries to match against /shop/movies/and/more/ ? A period (.) means "any character". A plus sign (+) means "one or more". Is your problem with requiring at least one character? Well, there is no category for "movie/and/more" Are you hinting at... RewriteRule /shop/([^/]+)(/)?$ /shop/product-catalog.php?category=$1 [L, QSA] Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577509 Share on other sites More sharing options...
requinix Posted May 3, 2020 Share Posted May 3, 2020 25 minutes ago, SaranacLake said: If the URL is always coming from a hyperlink then is it much concern? Is it, though? Always a link? And always coming from your website? 25 minutes ago, SaranacLake said: Are you wanting something like this? It's easier to just write /?, but yes. 25 minutes ago, SaranacLake said: Not sure I understand what you mean here... Spend some time learning about canonical URLs and what they mean to search engines. 25 minutes ago, SaranacLake said: Is your problem with requiring at least one character? Nope. I wanted you to describe, in technical detail, exactly how mod_rewrite is going to match a URL like /shop/movies/and/more/ against your regex. To understand what it's doing and what your syntax actually translates into. 25 minutes ago, SaranacLake said: Well, there is no category for "movie/and/more" True, but my point was that the category should probably just have been "movie". Right? The .+ matched everything up to the end, and not just the portion up to the next slash. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577510 Share on other sites More sharing options...
SaranacLake Posted May 3, 2020 Author Share Posted May 3, 2020 15 minutes ago, requinix said: Is it, though? Always a link? And always coming from your website? Okay, I see. 15 minutes ago, requinix said: It's easier to just write /?, but yes. Okay. 15 minutes ago, requinix said: Spend some time learning about canonical URLs and what they mean to search engines. Oh, right. I remember those now. I see your point. 15 minutes ago, requinix said: Nope. I wanted you to describe, in technical detail, exactly how mod_rewrite is going to match a URL like /shop/movies/and/more/ against your regex. To understand what it's doing and what your syntax actually translates into. True, but my point was that the category should probably just have been "movie". Right? The .+ matched everything up to the end, and not just the portion up to the next slash. I think you missed the end of my last response... Are you hinting at... RewriteRule /shop/([^/]+)(/)?$ /shop/product-catalog.php?category=$1 [L, QSA] I think that addresses your concern about "/movie/and/something/else/", right? Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577511 Share on other sites More sharing options...
requinix Posted May 4, 2020 Share Posted May 4, 2020 6 hours ago, SaranacLake said: I think you missed the end of my last response... Not exactly. You should be able to answer the question on your own now. Quote Link to comment https://forums.phpfreaks.com/topic/310679-mod_rewrite-questions/#findComment-1577515 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.