.htacces rewrite rule for subdomain like project1, project2 e.t.c - apache

i have many rules in my .htaccess file.
# project1.mysite.com
RewriteCond %{HTTP_HOST} project1.mysite.com$
RewriteCond %{REQUEST_URI} !^/project1
RewriteRule ^(.*)$ /project1/$1 [L]
# project554.mysite.com
RewriteCond %{HTTP_HOST} project554.mysite.com$
RewriteCond %{REQUEST_URI} !^/project554
RewriteRule ^(.*)$ /project554/$1 [L]
# project44.mysite.com
RewriteCond %{HTTP_HOST} project44.mysite.com$
RewriteCond %{REQUEST_URI} !^/project44
RewriteRule ^(.*)$ /project44/$1 [L]
e.t.c
i think what there is more smart rule by using only one mask.
subdomain like project{number} -> to folder like /project{number}
please advise a good solution

RewriteCond %{HTTP_HOST} (project\d+).mysite.com$
RewriteCond %{REQUEST_URI} !=/%1
RewriteRule ^(.*)$ /%1/$1 [L]
This should work as expected. Remove everything else from the file.

Related

How To exclude a Folder from 301 Redirect .htaccess

I have a 301 redirect on the domain goodereader.com that points to goodereader.com/blog/
I wanted to setup a forum, and wanted to exclude /forum/ from being 301'd
Here my current .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
<IfModule mod_security.c>
SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>
From the comments, you can fix this by adding the same condition to the 2nd rule. Rewrite conditions only get applied to the immediately following rule, they're not global in any way.
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/forum/.*$
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
or use a pass through:
RewriteEngine On
RewriteRule ^forum/ - [L]
RewriteCond %{HTTP_HOST} ^www\.goodereader\.com$ [NC]
RewriteRule ^(.*)$ http://goodereader.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^goodereader\.com$
RewriteRule (.*) http://goodereader.com/blog/$1 [R=301,L]
RewriteRule ^$ blog [L]
Make sure to clear your browser's cache. 301 redirects are permanent and the browser will always cache them.

htaccess Redirect Loop only on Home Page

All of a sudden this morning I'm having a problem with accessing the home page to my site.
I didn't make any changes so I'm not sure what the issue is. I'm guessing it has something to do with my .htaccess file.
All other pages to my site, I can access without a problem.
Below is my htaccess file. Any help would be appreciated. Thanks in advance.
Options -Indexes
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^hats-n-caps/(.*)$ http://www.domain-domain.com/apparel/hats-n-caps/$1 [R=301,L]
RewriteRule ^electronics/laser-pointers/(.*)$ http://www.domain-domain.com/desktop-office/laser-pointers/$1 [R=301,L]
#RewriteCond %{HTTP_HOST} !^www.domain-domain.com$ [NC]
#RewriteRule ^(.*)$ http://www.domain-domain.com/$1 [L,R=301]
RewriteCond %{REQUEST_URI} (^(.*)/site-map)
RewriteRule ^site-map$ /inc/pages/site-map.php [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/products-([0-9]+))
RewriteRule ^site-map/products-([0-9]+)$ /inc/pages/site-map.php?type=product&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/catalogs-([0-9]+))
RewriteRule ^site-map/catalogs-([0-9]+)$ /inc/pages/site-map.php?type=catalog&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)/site-map/pages-([0-9]+))
RewriteRule ^site-map/pages-([0-9]+)$ /inc/pages/site-map.php?type=pages&page=$1 [L]
RewriteCond %{REQUEST_URI} (^(.*)-p-(.*).html*)
RewriteRule ^(.*)-p-(.*).html$ product_info.php?products_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} (^(.*)-c-(.*).html*)
RewriteRule ^(.*)-c-(.*).html$ product_info.php?catalog_id=$2&%{QUERY_STRING}[L]
RewriteCond %{REQUEST_URI} !(^(.*)-c-(.*).html*|^(.*)-p-(.*).html*|^/index.php|^/sitemap.xml.*|^/info.php.*|^/google_base_feed.xml.*|^/BingSiteAuth.xml.*|^/test.html.*|^/test.*|^/ajaxplorer.*|/livechat.*|^/phpmyadmin.*|^/ControlPanel.*|^/actikare.*|^/amwater.*|^/phi.*|^/sas.*|^/blog.*|^/cfscg.*|^/chase.*|^/cleaver.*|^/clubz.*|^/medicone.*|^/net2ftp.*|^/college-net.*|^/ducks.*|^/fast-teks.*|^/gatesgroup.*|^/guesthouse.*|^/neighborcare.*|^/pdf.*|^/reedrill.*|^/survey.*|^/temp.*|^/templates.*|^/tidewater.*|^/tuthill.*|^/uploads.*|^/upload.*|^/_include.*|^/product_info.*|^/server_request.*|^/server_request_products.*|^/admin.*|^/img/.*|^/images/.*|^/spaw2?|^/static/.*|^/tmp/|^/_include/|^/robots.txt|^/LiveSearchSiteAuth.xml|^/favicon.ico|/SiteReturn/.*|^/inc/pages/site-map.php(.*)|^/carousel_products.php)
RewriteRule (.*)$ /index.php [L]
RewriteCond %{HTTP_HOST} ^domain-domain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.domain-domain.com$
RewriteRule ^/?$ "\/" [R=301,L]
RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
RewriteRule ^/?$ "\/" [R=301,L]
This line:
RewriteRule ^/?$ "\/" [R=301,L]
Is what's causing your redirect loop. Get rid of the entire rule:
# RewriteCond %{HTTP_HOST} ^domaindomain\.co$ [OR]
# RewriteCond %{HTTP_HOST} ^www\.domaindomain\.co$
# RewriteRule ^/?$ "\/" [R=301,L]
It essentially redirects requests to the root / to itself.

Apache .hatccess rewrites not working for legacy URLS

I'm trying to rewrite some legacy Joomla URLs on a site that's now using ExpressionEngine as its CMS but they're not working.
The ExpressionEngine URL rewrites, i.e. removing index.php from the URL work fine though.
This is what I've got:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
# This is the one that's not working
RewriteRule /index.php?option=com_chronocontact&Itemid=54 /contact/ [R=301,L]
# Force www
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Redirect index.php Requests
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
RewriteCond %{REQUEST_URI} ^/
RewriteCond %{QUERY_STRING} ^(gclid=.*)
RewriteRule ^(.+) /index.php?/ [L,PT]
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(assets|css|images|tinymce|js|min|cms|themes|index\.php|admin\.php|favicon\.ico|index\.php|path\.php|php\.ini) [NC]
RewriteRule ^(.+) /index.php?/ [L]
</IfModule>
Can anyone spot what I'm doing wrong?
The first thing is the stray RewriteCond %{HTTPS} !=on that you have at the top. It looks like it belongs to the rule under it, as in:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
As far as the rule that you have commented that doesn't work, the ? is a reserved character for regular expressions, and your pattern actually says that the second p in /index.php is "optional". Additionally, you can't match against the query string in a rewrite rule, you need to use a rewrite condition and match against the %{QUERY_STRING} variable:
RewriteCond %{QUERY_STRING} ^option=com_chronocontact&Itemid=54$
RewriteRule ^(index.php)?$ /contact/? [R=301,L]
is probably more along the lines of what you're looking for.

.htaccess mod_rewrite based on domain

I have the following rule in my htaccess, I need to modify it to add variable to the end of the re-written url. Basically I have two domains pointing to the same hosting, and I am showing different sites depending on which domain it used.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^(.*)$ /rrr/$1 [L]
If someone goes to rrr.me or www.rrr.me they get the index.php in the rrr directory of my website. This works great!
I now need to be able to add a variable to the index.php. So someone will type in rrr.me/abc and it will append the abc to the end of the index.php in this fashion index.php?var1=abc
NEW REWRITE RULES AFTER ADVICE FROM Olaf Dietsche IN COMMENTS.
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^.*$ /rrr/index.php?rrr=$0 [L]
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]
To append the requested URL as a query string
Rewriterule ^.*$ /rrr/index.php?var1=$0 [L]
To exclude the assets directory, you must use an additional RewriteCond
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
RewriteCond %{REQUEST_URI} !/assets/
Rewriterule ^(.*)$ /rrr/$1 [L]
For the assets files, you can use a similar redirect
RewriteCond %{HTTP_HOST} ^(www\.)?rrr\.me
RewriteCond %{REQUEST_URI} !^/rrr
Rewriterule ^assets/.*$ /rrr/$0 [L]

.htaccess rewrite without www AND redirect to subdirectory

I'd like to redirect
www.example.com/* to example.com/*
And at the same time redirect
example.com/* to example.com/forum/*
But I also have /wiki/ and /blog/ and /style/, so I don't want to redirect
example.com/style/* to example.com/forum/style/*
This is what I have at the moment, which is not working quite correctly:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/forum/
RewriteRule ^(.*)$ forum/$1 [R=301,L]
Clarification: my question can be asked in a simpler way.
I'd like to redirect an empty REQUEST_URI or /, or a non-existent file only if it is in the root directory to /forum/.
Try this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=301,QSA,L]
RewriteCond %{REQUEST_URI} !^/(wiki|blog|style|forum)
RewriteRule ^(.*)$ http://www.example.com/forum/$1 [R=301,QSA,L]
I would use these rules:
# redirect www.example.com to example.com
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^ http://example.com%{REQUEST_URI} [L,R=301]
# prefix everything but /forum/, /wiki/, /blog/, /style/ with /forum/ and rediret to it
RewriteRule !^(forum|wiki|blog|style)/ /forum%{REQUEST_URI} [L,R=301]
The second rule could additionally be replaced by this one to check the existence of the first path segment for every request.
# check if first segment of requested URI path is either missing
RewriteCond $0 ^$ [OR]
# or cannot be mapped to an existing directory
RewriteCond %{DOCUMENT_ROOT}$0/ !-d
RewriteRule ^[^/]* /forum%{REQUEST_URI} [L,R=301]
I'd say this should work.
RewriteEngine on
RewriteRule ^forum/(.*)$ forum/$1 [L]
RewriteRule ^wiki/(.*)$ wiki/$1 [L]
RewriteRule ^blog/(.*)$ blog/$1 [L]
RewriteRule ^style/(.*)$ style/$1 [L]
RewriteRule ^(.*)$ forum/$1 [L]
RewriteCond %{HTTP_HOST} ^www.example\.com$
RewriteRule ^(.*)$ http://example.com/$1
I don't have the answer for everything but for your www/no www problem you could try this :
# Enforce www
# If you have subdomains, you can add them to
# the list using the "|" (OR) regex operator
RewriteCond %{HTTP_HOST} !^(www|subdomain) [NC]
RewriteRule ^(.*)$ /exemple/$1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ http://exemple.com/$1 [L,R=301]