htaccess add index.htm at end of folder - .htaccess

I need a way to add index.htm at end of url.
For example:
www.example.com/folder1/folder2 AND
www.example.com/folder1/folder2/
must be redirected internally to www.example.com/folder1/folder2/index.htm (this must apply for any number of folders/subfolders)
My htaccess (at root) so far looks like:
Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /
#
# some 301 REDIRECTS here
#
# rewrite non-www into www
# after all redirection rule AND before any rewrite rule
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
#
# rewrite rules below here
#
Any way to fix this?
Thank you.
EDIT:
- All urls come from rewrite rules - there are no actual folders in site.
- Typing www.example.com/folder1/folder2 in my browser, only get a 404 page not found. I need it to redirect to www.example.com/folder1/folder2/index.htm (there is a rule creating this url)

Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.htm$ [NC]
RewriteRule ^(.*?)/?$ /$1/index.htm [L,R=301]

Do you mean you want to REDIRECT /folder1 and /folder1/ into /folder1/index.htm, and /folder1/folder2 and /folder1/folder2/ to /folder1/folder2/index.htm, and /folder1/folder2/folder3 and /folder1/folder2/folder3/ to /folder1/folder2/folder3/index.htm
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/index.htm [R]
RewriteCond %{REQUEST_URI} ^/([a-z0-9-_]+)/([a-z0-9-_]+)/([a-z0-9-_]+)/?$
RewriteRule ^(.*) /%1/%2/%3/index.htm [R]

Related

htaccess rewrite from subdomain (root) to www (root) without 301 redirect

I am having error in rewriting url. I want the request
http://go.example.com/all-pathnames-flenames
should be handled by some page
http://www.example.com/myfile.php
It should not be hard or 301 redirect (rather original url should be there).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^go\.example\.com$ [NC]
RewriteRule ^(.*)$ /myfile.php [L]
above mentioned is the code i have but it is not working
You need to exclude rewriting myfile.php, otherwise you'll create a loop:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^go\.example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/myfile.php
RewriteRule ^(.*)$ /myfile.php [L]

.htaccess rewrite: redirect all URLs except home URL and assets

I would like all URLs except my root URL (/ and /index.php) and the JS/CSS and image assets it uses, to redirect to another URL. All assets are contained in the /assets directory
Here's what I have so far but it's not working
RewriteEngine on
RewriteRule ^(.*)\.(?!js|css)([^.]*)$ $1\.php
RewriteCond %{REQUEST_URI} !^.assets$
RewriteCond %{REQUEST_URI} !/index.php$
RewriteCond %{REQUEST_URI} !/style.php$
RewriteCond %{REQUEST_URI} !/$
RewriteRule $ http://berlincallingny.eventbrite.com [R=302,L]
The rules I need are:
do not rewrite any URL containing "assets"
Do not rewrite index.php
Do not rewrite style.php
Do not rewrite the root URL
do not rewrite any URL containing .css or .js
Thank you!
Your regex for assets directory is not correct here:
RewriteCond %{REQUEST_URI} !^.assets$
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/assets(/.*|)$ [NC]
RewriteCond %{REQUEST_URI} !/(index|style)\.php$ [NC]
RewriteRule ^.+$ / [R=302,L]

URL rewrite not working as expected

I am using the following code in .htaccess file
DirectoryIndex home.php
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^$ - [L]
RewriteRule ^/?([^\./]*)[:;,\.]*$ $1.php
RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]*)$ listing.php?business=$2
This code hides the .php extension of the PHP files in the root folder only, But I want to apply it to the PHP files in all subfolders.
I also want to deny access using the original URL , that is the URL includes the .php extension.
I also want the third rule should be worked.
Replace your code with this:
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^([a-zA-Z0-9_,-]+)/([0-9]+)$ listing.php?business=$2 [L,QSA]
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php[\s\?] [NC]
RewriteRule ^ - [F]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ /$1.php [L]

Rewrite rule to work consistently for subdomain and subdirectory

I have a nice rule for rewriting the url to /meetings, but the rule doesn't rewrite the url properly when visited from a subdirectory.
EDIT: My .htaccess file is in the subdirectory /blog
When I use this rule:...
RewriteRule ^meetings$ /?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): 404 error
When I use this rule:...
RewriteRule ^meetings$ http://blog.example.com/?page_id=2809 [L]
When visited from subdomain(blog.example.com/meetings): blog.example.com/meetings
When visited from subdirectory(example.com/blog/meetings): blog.example.com/?page_id=2809
Is there a way to get the rule to work consistently for the subdomain and the subdirectory?
You may try this in one .htaccess file in /blog directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /blog
RewriteCond %{HTTP_HOST} blog\.example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /?page_id=2809 [L,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com [NC]
RewriteCond %{QUERY_STRING} !page_id= [NC]
RewriteRule ^meetings /blog/?page_id=2809 [L,NC]
For permanent redirection, replace [L,NC] with [R=301,L,NC].

htaccess for www-only redirection

Is there any way I can redirect my urls to only use http://www.domain.com instead of http://domain.com?
Here's my htaccess but doesnt work:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
# RewriteBase /test/
Options +FollowSymlinks
# Protect application and system files from being viewed
RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
#redirec
#detect urls without www
RewriteCond %{http_host} ^thehotelinventory.com [NC]
#redirect to www, does not work though
RewriteRule ^(.*)$ http://www.thehotelinventory.com/$1 [R=301,L]
AddHandler php5-script .php
If you need to redirect from non-www to www, then redirect to HTTPS for certificates like THAWTE that dont support www non-www on SSL 123.
RewriteEngine On
#non-www to www
RewriteCond %{HTTP_HOST} ^yourdomain.com
RewriteRule (.*) http://www.yourdomain.com/$1 [R=301,L]
#Redirect to HTTPS before www redirect
RewriteEngine on
RewriteCond %{HTTPS}
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Thanks to Brasil na Web Dev. Guys.
a simple answer can be found here: http://snippets.dzone.com/posts/show/2264

Resources