.htaccess cook book, examples, and favourites

HT Access

All of My Favourite .HTAccess Examples

Wildcard Domain Example #1

This will take your URL request, for anything.dummysite.com and redirect to dummysite.com/anything/index.html.  Combined with an CNAME Record for @

#### URL Rewrite Handler for Subdomains (by Randall Krause) ####
RewriteCond %{HTTP_HOST} !^www\.dummysite\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.dummysite\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]

Wildcard Domain Example #2

What this does is if user visits: test.example.com, it will show contents of file: example.com/user/profile.php?name=test. If someone goes to lol.example.com, it will show page: example.com/user/profile.php?name=lol but the URL will remain the same with the subdomain, like test.example.com and lol.example.com.
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ([^.]+)\.example\.com [NC]
RewriteRule ^/?$ /user/profile.php?name=%1 [L]

Single Argument Example

## example:  http://dummysite.com/cities/Westwood.html
## example:  http://dummysite.com/cities/Santa-Monica.html
RewriteRule ^cities/([a-zA-Z0-9-_]+).html$ /city-search.php?city=$1

Double Argument Example

RewriteRule ^newsreader/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)$ /readnews.php?id=$1
RewriteRule ^newspreview/([a-zA-Z0-9-_]+)/([a-zA-Z0-9-_]+)$ /previewnews.php?id=$1

Set Access Control To Allow Mixed Content

<IfModule mod_headers.c>
  <FilesMatch "\.(ttf|ttc|otf|eot|woff|woff2|font.css|css|js)$">
    Header set Access-Control-Allow-Origin "*"
  </FilesMatch>
</IfModule>

References:

https://serverfault.com/questions/841624/htaccess-wildcard-subdomain-path

Comments

Popular posts from this blog

Microsoft Visio 2010 Premium Product Keys

Fix: The Diagnostic Service Host service failed to start due to the following error. [ solved, no kidding ]

Mercedes Benz Diesel CDI EGR Emulator Circuit Diagrams