StevenOliver Posted July 13, 2020 Share Posted July 13, 2020 Any suggestions for a good tutorial where I can learn how to write my own mod_rewrite (and mod_alias) code? For example, here is a simple RewriteRule: RewriteRule ^/?(.*) https://www.example.com%1/$1 I would like to change it to a corresponding "Redirect" directive, but obviously, the next line doesn't work (although it looks like it should) Redirectmatch ^/?(.*) https://www.example.com%1/$1 I'd like to learn what I'm doing, rather than being a "copy-and-paster" all my life 😀  Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/ Share on other sites More sharing options...
requinix Posted July 13, 2020 Share Posted July 13, 2020 1. Read the documentation for whatever directives you want to use. 2. The term you are looking for is "regular expression". There are many places where you can learn about them. Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579600 Share on other sites More sharing options...
StevenOliver Posted July 14, 2020 Author Share Posted July 14, 2020 The documentation for mod_alias and mod_rewrite does not explain %, ^, etc., and it is impossible to google certain symbols, hence my question if anyone knew of a good tutorial. Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579602 Share on other sites More sharing options...
requinix Posted July 14, 2020 Share Posted July 14, 2020 3 minutes ago, StevenOliver said: The documentation for mod_alias and mod_rewrite does not explain %, ^, etc., and it is impossible to google certain symbols, hence my question if anyone knew of a good tutorial. The documentation for mod_rewrite does explain what %{} means. ^ is not part of mod_rewrite per se but part of the syntax for regular expressions. Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579604 Share on other sites More sharing options...
StevenOliver Posted July 14, 2020 Author Share Posted July 14, 2020 I've rephrased my original post. "Any suggestions for a good tutorial where I can learn  how to   the symbols needed to write my own mod_rewrite (and mod_alias) code?" Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579605 Share on other sites More sharing options...
requinix Posted July 14, 2020 Share Posted July 14, 2020 Yes: the mod_rewrite documentation and any place that explains regular expressions. You're then expected to take all the knowledge you learn and apply it towards whatever ends. I can provide you with links to Google, if you'd like. Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579606 Share on other sites More sharing options...
StevenOliver Posted July 16, 2020 Author Share Posted July 16, 2020 I'm still waiting for my google links 😀 I'm trying to figure out what the % means in %{HTTP_HOST} and trying to figure out when to use the != 80 vs !https there are a thousand ways to do things, one is always better than the other. Trust me, there are NO tutuorials on how to do htaccess regex. The "documentation" might give a cursory example, and so basically it takes me about a full 8 hour day to learn each symbol. And, things like "redirectpermanent" is written sometimes, and "redirect permanent" is written sometimes. And sometimes it will go like redirect "/" "www.blah.com" and sometimes there will be no quotes... and they both work. That's not fair. Shouldn't have more than one way to do things. There should be a big tutorial, all on one page, with all that stuff on it. And, just because one knows a little regex, that doesn't mean you can suddenly write your own rewrite rules, because there are secrets..... big secrets... I'm getting to old to keep being a "copy-and-paster" where someone's one-page blog with ads all over the page for their stupid out-of-date GitHub page LOL. I need to learn and KNOW this stuff for myself :-) Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579662 Share on other sites More sharing options...
requinix Posted July 16, 2020 Share Posted July 16, 2020 43 minutes ago, StevenOliver said: I'm trying to figure out As long as you have a few specific questions that can't be easily answered through documentation or a search engine.  43 minutes ago, StevenOliver said: what the % means in %{HTTP_HOST} The only thing it "means" is that the name inside the %{...} is a variable. What variables you have available are listed in the documentation.  43 minutes ago, StevenOliver said: and trying to figure out when to use the != 80 vs !https  There's basically no reason to write 80 or 443. Those are port numbers. What you should be caring about is whether the request is HTTPS or not.  43 minutes ago, StevenOliver said: there are a thousand ways to do things, one is always better than the other. Not necessarily. Some practices are outdated, some are harder to read and understand than others, some are dumb, and occasionally some are wrong. But the main criteria for deciding which to use is "does it do what you need it to do".  43 minutes ago, StevenOliver said: Trust me, there are NO tutuorials on how to do htaccess regex. Are you trying to tell me that if you put "htaccess regex tutorial" into Google you won't find anything relevant? Before you answer, consider that I am bothering to spend the time writing this sentence to refute your statement.  43 minutes ago, StevenOliver said: The "documentation" might give a cursory example, and so basically it takes me about a full 8 hour day to learn each symbol.  If it takes you "a full 8 hour day" to learn what %{HTTP_HOST} means then I'm not sure anyone can help you. Maybe the problem is that you're not learning in an efficient way? Memorizing "percent sign then opening brace then HTTP_HOST then closing brace means it uses the HTTP_HOST variable" is going to waste a lot of brain cells if you have to repeat that for every single variable available.  43 minutes ago, StevenOliver said: And, things like "redirectpermanent" is written sometimes, and "redirect permanent" is written sometimes. Yes, that's right.  43 minutes ago, StevenOliver said: And sometimes it will go like redirect "/" "www.blah.com" and sometimes there will be no quotes... and they both work.  You know how in SQL there are database names and table names and column names and all that? You know how you can quote them, but don't actually need to unless the name is a special keyword or has some unexpected symbol in it? Same for Apache's configuration files. Quotes are mostly about when you need to write something that has a space. The quotes make sure the whole thing gets treated as a single value instead of multiple values. If you don't have spaces then you don't need quotes.  43 minutes ago, StevenOliver said: Shouldn't have more than one way to do things. I'm sorry you feel that way but 99% of the programming world does not work like that.  43 minutes ago, StevenOliver said: There should be a big tutorial, all on one page, with all that stuff on it. And there should be a single year of school where teachers tell me everything I need to know about life. And there should be a single driver's ed. class that tells me everything I need to know about driving vehicles. And there should be a single politician who manages everything about a country. But there isn't. Because stuff is complicated and it can't all be covered in one place at one time. Typically people understand this concept early in life and then learn how to get information from multiple sources to ensure you're getting everything you need. (Unless you're American.) If you keep looking for a single resource that answers all your questions then you're not going to find one. Learn what you can from where you can then move on to the next one, and keep doing that until you don't have any more questions.  43 minutes ago, StevenOliver said: And, just because one knows a little regex, that doesn't mean you can suddenly write your own rewrite rules, because there are secrets..... big secrets... That's true. Which is why you can't learn everything about mod_rewrite in one place. You need to learn regular expressions and understand a bit about Apache's configuration files and learn to a reasonable degree how mod_rewrite works and know how to test and troubleshoot what you've done if there's a mistake. The good news is that you're not expected to pick this all up immediately. Learning takes time. The bad news is that nobody can learn this stuff for you. Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579665 Share on other sites More sharing options...
kicken Posted July 16, 2020 Share Posted July 16, 2020 Just remember that the stuff you find in .htaccess files are just Apache configuration directives. So when you're unsure of something the thing to do is to visit the Apache directive index and find the directive in there to find the full documentation on it. Most of the time, this will give you the information you need. For example: 3 hours ago, StevenOliver said: I'm trying to figure out what the % means in %{HTTP_HOST} From the RewriteCond documentation Quote Server-Variables: These are variables of the form %{ NAME_OF_VARIABLE } where NAME_OF_VARIABLE can be a string taken from the following list: So the %{ means it's a server variable, as opposed to some other variable types.  3 hours ago, StevenOliver said: when to use the != 80 vs !https The syntax of conditions is covered in the same RewriteCond documentation. !=80 for example is covered by Quote CondPattern is usually a perl compatible regular expression, but there is additional syntax available to perform other useful tests against the Teststring: You can prefix the pattern string with a '!' character (exclamation mark) to negate the result of the condition, no matter what kind of CondPattern is used. [...] =CondPattern Lexicographically equal Treats the CondPattern as a plain string and compares it lexicographically to TestString. So it means (Not)(Equals 80) As for what condition to write, this mostly just requires you to use your brain. What condition you use depends on what you're testing against. If you're testing against %{SERVER_PORT} then the value will be a number so you'd check for =80 or =443 for example. It wouldn't make sense to check for the string 'https' in that case.  Likewise the parts that require regular expressions require you to use your brain to come up with the correct expression. If you don't know regular expressions very well or at all, then you'll need to spend time learning that. Regular expressions are not just an Apache thing so their manual doesn't really cover them in detail. It does at least give you the basics and what you need to know to look for further help: The term "Regular expression"; Links to documentation and additional help.  All in all, Apache has pretty good documentation. If you're struggling with it, you'll be in for a world of hurt when it comes time to deal with things that have much less well-defined documentation.  Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579668 Share on other sites More sharing options...
StevenOliver Posted July 16, 2020 Author Share Posted July 16, 2020 (edited) Requinix, thank you! All your answers make sense.... what happens to me is I set out to write a block of code, but then I go off on two tangents. Then from each of those two tangents, I go onto two more tangents, etc., By the end of the day, I have 30 forked tangents, with 30 browser windows open with tutorials on everything 😀 Kicken, thank you! I have your suggested documentation open in another browser window right now. I'm going to read it now. Regarding simple things like making sure HTTPS is on or off, well, I do have the brain power, but I don't have the lexicon/context of years of PHP learning to know which is the best way to do something. For example: a.) RewriteCond %{HTTPS} off b.) RewriteCond %{ENV:HTTPS} !on c.) RewriteCond %{SERVER_PORT} 80 But anyway, I'm gonna read me some Apache Docs! 😀 Edited July 16, 2020 by StevenOliver Quote Link to comment https://forums.phpfreaks.com/topic/311079-where-to-learn-mod_rewrite-language/#findComment-1579681 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.