Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I make a custom URL parser with Apache?

I heard this can be done with the web.config file. I want to make it so, for instance, my URL http://help.BHStudios.org/site might go to http://BHStudios.org/help.php?section=site, or http://i.BHStudios.org/u3Hiu might redirect to some other URL stored in a database with the hash u3Hiu as the key, or if something goes wrong and the internal file structure is exposed like http://Kyli.BHStudios.org/http/bhstudios/v2/self/index.php (something that happens with GoDaddy's servers for whatever reason) it'll change it to its intended URL http://Kyli.BHStudios.org before that's exposed tot he user.

Since I've never done this before, could you please also explain why you gave the answer you did?

like image 831
Ky. Avatar asked Feb 11 '26 15:02

Ky.


1 Answers

A few Apache mod_rewrite rules in either your servers httpd.conf or in a .htaccess file, in your htdocs directory will do the majority of what you want e.g.

RewriteEngine On
RewriteBase  /

# Default Rule - for non physical objects (not a file or directory): 
# Internally rewrite (user won't see the URL) to /index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php [L]

#If the Browser request contains a .php, instruct the browser to remove it.
RewriteCond %{THE_REQUEST}      \.php      [NC]
RewriteRule ^/?(.*)\.php$       http://%{HTTP_HOST}/$1         [R=301,NC,L]

# Specific rule
RewriteRule ^/?site   /help.php?section=site 

The masking of real file system objects will not be perfect, and slightly pointless, as a user just needs to right click and view source on any served page, to obtain the actual URL's.

like image 192
arober11 Avatar answered Feb 15 '26 20:02

arober11



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!