Rewrite rule to work consistently for subdomain and subdirectory - .htaccess

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].

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]

.htaccess rewrite to subfolder with codeigniter rewrite

I have a site that needs to exist in a subfolder
example.com/site
But i'm trying to use the .htaccess to remove any links that contain www (to make sure codeigniter csrf doesn't throw errors), so i've added
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
This works well when there is a page identifier specified, so
www.example.com/site/book rewrites to example.com/site/book
But when there is no page identifier specified I get a 404
www.example.com/site rewrites to example.com/site//usr/local/pem/vhosts/103480/webspace/httpdocs/new
I was wondering if anybody could point me in the right direction?
This is my full .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.example\.com)?$
RewriteRule ^(.*)$ http://example.com/site/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
You may try this instead:
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{REQUEST_URI} ^/(.*) [NC]
RewriteRule .* http://example.com/%1 [R=301,L]
Maybe, you're just missing a RewriteBase
Depending on where the .htaccess file is, try either
RewriteBase /
or
RewriteBase /site
Never test with 301 enabled, see this answer
Tips for debugging .htaccess rewrite rules
for details.

htaccess add index.htm at end of folder

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]

htaccess redirect subdomain

I have the following .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
The problem is that all subdomains get redirected to www.mysite.co/subdomain
How can I aviod this?
Your rewrite rule logic in plain speak is doing the following:
For ANY HOST other thant www.mysite.co (including foo.mysite.com, blog.mysite.com, etc..)
Permanent Redirect (301) to http://www.mysite.co/
This is the last rule to check in the .htaccess file, so bail out
To not redirect your own subdomains, the easiest and clearest way is to handle it explicitly with more rewrite conditions (see example below). For more kung fu htaccess fun try this: .htaccess Wildcard Subdomains
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^blog.mysite.co$ [NC]
RewriteCond %{HTTP_HOST} !^foo.mysite.co$ [NC]
RewriteRule ^(.*)$ http://www.mysite.co/$1 [R=301,QSA,L]
</IfModule>
You need to run your index.php file
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www\.)?([a-z0-9-]+)\.example\.com$ [NC]
RewriteRule !^index\.php($|/) index.php/accounts/%2%{REQUEST_URI} [PT,L]

Resources