From Code Trash

Jump to: navigation, search

These are my experiments with .htaccess


Rewrite folder name to a filename

I have a project which has the admin url as folder/cms-admin/index.php and i want to access this as folder/admin by which it should rewrite to the folder/cms-admin/index.php and it worked...

RewriteRule ^admin cms-admin/index.php [R]

Now i am trying to find a way to include admin/ so that ^admin/ will also be redirected to the above mentioned url. could some body suggest?

All requests to whatever.htm will be sent to whatever.php

This will rewrite any filename with .htm to .php This is useful when you change a file from static htm,html to a php file. So the search engine will not show file not found or any other link backs.

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)\.htm$ $1.php [NC]

The [NC] part at the end means "No Case", or "case-insensitive";

All files of .htm to .php

RewriteRule ^(.+)\.htm$ http://site.com/$1.php

Rewrite www to non www url

RewriteCond %{HTTP_HOST} ^www.sitename.com [NC]
RewriteRule ^(.*)$ http://sitename.com/$1 [L,R=301]

RewriteCond %{HTTP_HOST} ^www.sitename.com [NC]
RewriteRule ^(.*)$ http://sitename.com/folder/$1 [L,R=301]

Rewrite non www to www

RewriteCond %{HTTP_HOST} ^sitename\.com
RewriteRule (.*) http://www.sitename.com/$1 [R=301,L]

Rewrite non www to www with https

Here all non www url will be rewrited to www with the https prefix
For example if i give sitename.com this will be redirected to https://www.sitename.com

RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.sitename.com/$1 [R,L]

RewriteCond %{HTTP_HOST} ^sitename\.com
RewriteRule (.*) https://www.sitename.com/$1 [R=301,L]

Redirect http to https on one page only

RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^contact/?$ https://secure.example.com/contact/ [R=301,L]
This will redirect either

Another Example from http://www.besthostratings.com/articles/force-ssl-htaccess.html

RewriteEngine On 
RewriteCond %{SERVER_PORT} 80 
RewriteCond %{REQUEST_URI} somefolder 
RewriteRule ^(.*)$ https://www.domain.com/somefolder/$1 [R,L]

Rewrite file links

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^files/([^/]+)/([^/]+).zip /download.php?section=$1&file=$2 [NC]

would allow you to present this link as..
and in the background have that transparently translated, server-side, to..

From: http://corz.org/serv/tricks/htaccess2.php

Rewrite http to https for only one folder

This is like in the press page sub folder store

RewriteEngine on
DirectoryIndex index.php index.html
RewriteCond %{SERVER_PORT} 80 
RewriteRule ^(.*)$ https://www.sitename.com/store/$1 [R,L]

Youtube in HTTPS page

In your html code you have to do it like the following

<embed ... src="https://www.youtube.com and so and so ...

In the htaccess file you have to do it like the following

RewriteRule ^youtube/(.*)$ http://www.youtube.com/$1 [L]

so all https of youtube links will be redirected to http but still the page loads without any security warnings like in IE.

Reference: http://www.adammershon.com/display-youtube-videos-on-ssl-page/


Personal tools
Google AdSense