Any goats got a moment to help me out with Apache (.htaccess) url rewriting?
1 14 Mar 2020 04:29 by u/argosciv
Got the results I mostly wanted on IIS, but I fucking suck at manual url rewriting (Apache).
Trying to get
site.com/index.php?action=anyaction&view=anyview
to rewrite to
site.com/anyaction/anyview
helpful example with explanation would be much appreciated.
Note that anyaction and anyview can and should be literally be any value (the rest of the code handles whether or not the request is valid and serviceable).
TIA.
29 comments
0 u/PuttitoutIsGone 14 Mar 2020 04:41
No link. Fuck off.
0 u/BillyLuath 14 Mar 2020 04:57
No link needed, he's asking about rewrite rules.
0 u/PuttitoutIsGone 14 Mar 2020 04:59
He's a firm believer that if a post doesn't contain a link, it should be deleted.
0 u/BillyLuath 14 Mar 2020 05:03
Oh, Argo, what ever happened to him?
0 u/argosciv [OP] 14 Mar 2020 20:31
You really gonna buy PiG's spin here?
This subverse has no rule requirements for a link to be present in submissions.
Subverse rules:
And wouldn't you know it, PiG and "Cransch" broke rule 1.
0 u/Cransch 14 Mar 2020 05:03
Would you expect anything else from a hypocrite jew faggot?
0 u/BillyLuath 14 Mar 2020 05:00
Forget using .htaccess, create a config.php file, require it once on every request and do your $_GET magic there.
Your talking about aliasing.
0 u/argosciv [OP] 14 Mar 2020 05:05
So you're suggesting:
?
0 u/BillyLuath 14 Mar 2020 05:11
No, you've just replaced the query variables without disrupting the query order.
If the order stays the same, then it doesn't matter what the variable is.
This is an Alias. You're just translating the query.
0 u/argosciv [OP] 14 Mar 2020 05:15
One or both of us isn't understanding the other...
See the edited stipulations in the main post while I get back to you with some screenshots from my IIS deployment.
0 u/argosciv [OP] 14 Mar 2020 05:58
Screenshots demonstrating reverse compatible url rewriting via IIS:
I'm hoping to achieve the same result on Apache (preferably with .htaccess, unless a better approach can be explained).
0 u/BillyLuath 14 Mar 2020 16:01
Yeah, in a different programming language I worked in (not naming it) this is easy as pie, but in PHP, not so much.
I can see why you are focusing on doing rewrites in the .htaccess file instead of just creating a redirect engine that parses all URLs and builds the friendly URL.
Here's an interesting article that explains exactly what you are looking for:
http://corz.org/server/tricks/htaccess2.php
0 u/argosciv [OP] 14 Mar 2020 16:06
Fucking beautiful, exactly what I needed.
Yep, after I get the hang of this site deployment (as a template), I'm going to try to reproduce it in .NET and maybe others.
0 u/argosciv [OP] 14 Mar 2020 17:23
Dude, you're a freakin' legend...
Now all I have to do is figure out reverse compatibility via .htaccess OR do some $_GET magic to turn a classic url into a pretty url and redirect using header("Location: ~").
0 u/BillyLuath 14 Mar 2020 18:17
Have you tried: parse_url
https://www.php.net/manual/en/function.parse-url.php
0 u/argosciv [OP] 14 Mar 2020 18:49
I'm sure I've messed around with that in the past. Doing the reverse compat through php isn't going to be difficult, I'm just fucking around with .htaccess for now to get a feel for which rules work, don't work, and work best for me with the least amount of mess.
Reverse compat can wait :)
0 u/argosciv [OP] 15 Mar 2020 15:54
Update:
Worked out how to prettify urls via php :)
0 u/BillyLuath 15 Mar 2020 16:02
Cool beans, glad you got it working.
0 u/argosciv [OP] 15 Mar 2020 16:08
Thanks again for the link to that htaccess guide. Still got a lot of learning there so I can reduce mess, but I think @cantaloupe6's comment might help with that (gotta learn what RewriteBase is for).
I was able to, on the php side, make site.com also redirect to site.com/home. I figure this is also possible via htaccess but now I'm hooked on .NET Core xD
0 u/argosciv [OP] 15 Mar 2020 15:54
O_O
php can suck a sick
.NET Core is fucking amazing when using Visual Studio.
@PuttItOut
0 u/BillyLuath 15 Mar 2020 16:07
I don't have a lot of experience with IIS deployments so never got into .NET, except to fix problems for someone that had a legacy app.
I actually prefer JSP or Ruby (not RAILS!) over PHP, and one supposedly obsolete language that rocks them all for rapid deployment.
0 u/argosciv [OP] 15 Mar 2020 16:11
According to documentation, the best approach is to use a kestrel "reverse proxy" deployment (request <-> IIS / Apache / Nginx <-> Kestrel <-> .NET).
0 u/SpottyMatt 14 Mar 2020 06:06
He wants the URL rewrite for search engine optimization, probably. So the pretty site.com/anyaction/anyview URLs can be indexed and the contextual information in the URL retained.
0 u/BillyLuath 14 Mar 2020 15:48
I thought it was more for security, by hiding the structure of the application. Search engines have been able to index query strings for over 10 years now.
0 u/argosciv [OP] 14 Mar 2020 20:27
It's a bit of both really. Not so much worried about search engines, more user-friendliness, plus a bit of obfuscation (not that it would be too difficult to figure out what the underlying query is).
@SpottyMatt
0 u/Symptomatic 02 Apr 2020 23:01
I concur also there is a simple concept of implementing functionality on the lowest part of the stack as feasible. In this case you could do it with a firewall or more simply and quickly in the web server. Putting into an interpreted [even though cached] construct is an extra layer imho.
0 u/thebearfromstartrack 14 Mar 2020 05:02
What is the grammar for search and substitute like unix/vi has? for example 1,$s/poop/cake/gp (substitute every occurance of any word with poop in it with "cake" and show results.
0 u/cantaloupe6 15 Mar 2020 03:33
Something like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^news/(.*) /news?item=$1 [R=301,L]
</IfModule>
0 u/obvious-throwaway- 19 Mar 2020 20:48
If you are just starting your website, you might consider going a completely different direction. It's been awhile, so I'm a little rusty on terms and the exact workings of it, so hopefully someone here can correct me on wrong terminology and workings without being a complete dick, but here goes,
JSON is great direction to go with websites. If you are unfamiliar with it, you could consider using jQuery to implement it. Hopefully this doesn't turn into a conversation of jQuery and it's bloat, but if you hate it for some reason, just use Javascript.
How it works is that you only have one URL, "https://mywebsite.com/" and every page of your website is accessed through that. It makes for a faster and less bloated website as you never have to change your Headers or meta tags, although you can.
How it works is that you only load the internal parts of the webpage instead of reloading the entire webpage every single time something changes. This is exactly how messaging works on Voat. When you post a comment, it doesn't reload the entire page, just adds the comment, this is JSON.
If you design properly from the start and can keep it all straight in your head, you can easily create an entire website based around JSON. Bookmarking can still be done, you can use hashtags (anchor links) to allow for carrying variables between pages if you want a page to be bookmarkable or sharable as a link. So, "https://mysite.com/#user1113-page3-section5". Just pick a format and use the explode function or whatever you like to break apart the hashtag data.
It's a really neat way to build a clean, fast and slim website. It also allows you complete control of how users interact with your website in code without the necessity of using an outside toolset like .htaccess or config.php