.htaccess infinite loop how to stop - .htaccess

I have several .htaccess rules I would like to combine. Here's the gist of what I want to accomplish:
jethro.mysite.com -> Redirects to -> gallery.mysite.com/?username=jethro&page_name=Home
(note: page_name is = Home if not already specified)
jethro.mysite.com/ajax/dir/script.php -> Redirects to -> gallery.mysite.com/ajax/dir/script.php
Anything else is processed like normal. Example: jethro.mysite.com/includes/blah.css -> Redirects to -> gallery.mysite.com/includes/blah.css
So far my .htaccess located at jethro.mysite.com looks like this, but I get an infinite loop:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^jethro.mysite.com [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule (.*) http://jethro.mysite.com/?username=jethro&page_name=Home [P]
RewriteCond %{QUERY_STRING} page_name=script
RewriteRule (.*) http://gallery.mysite.com/ajax/dir/script\.php [P]
RewriteCond %{QUERY_STRING} page_name=(.*)
RewriteRule (.*) http://gallery.mysite.com/?username=jethro&page_name=%1 [P]
If I disable the 5th and 6th lines above it will no longer do the loop. Please help how do I combine these ideas? Thanks!

Try the following.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^jethro.mysite.com [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule . http://jethro.mysite.com/?username=jethro&page_name=Home [P,L]
RewriteCond %{QUERY_STRING} page_name=script [NC]
RewriteRule . http://gallery.mysite.com/ajax/dir/script\.php [P,L]
RewriteCond %{QUERY_STRING} page_name=(.*) [NC]
RewriteCond %1 !=script [NC]
RewriteRule . http://gallery.mysite.com/?username=jethro&page_name=%1 [P,L]
I also assume that the [p] is intentional i.e. you want to proxy the request.

Related

RewriteCond for multiple RewriteRules

currently, I have the following .htaccess-file.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^ index.php?file=index [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^language/?$ index.php?file=language [NC,L,QSA]
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^about-us/?$ index.php?file=about_us [NC,L,QSA]
As you can see, I have to copy the following 2 lines for every RewriteRule:
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
This works, but I want to know if there is a better solution?
What I want?
For example, when a user visits http://{subdomain-here}.example.com/about-us, the page index.php?file=about_us must show up. For every subdomain, except for the subdomains javascript, css and images.
Edit 1: If a visitors goes to http://javascript.example.com/about-us, the browser should give a 404 error.
Edit 2: a "general" regex (e.g. (.*)) will not work because the name of the file in the URL is not always the same as the original filename. http://subdomain.example.com/about-us should point to index.php?file=about_us (see the _) and not to index.php?file=about-us.
You can apply the opposite logic
RewriteEngine on
# Don't touch anything when it comes to those 3 subdomains
RewriteCond %{HTTP_HOST} ^(?:javascript|css|images)\.example\.com$ [NC]
RewriteRule ^ - [L]
# If we reach this part, this means we're not in the 3 subdomains restriction
RewriteRule ^$ /index.php?file=index [NC,L]
RewriteRule ^language$ /index.php?file=language [NC,L]
RewriteRule ^about-us$ /index.php?file=about_us [NC,L]
You can see that as an equivalent to
if subdomain in (javascript, css, images)
return
do something here for other subdomains
You can use this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^([a-z-]+)\.example\.com$ [NC]
RewriteCond %1 !^(javascript|css|images)$ [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]
or the following
:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(javascript|css|images) [NC]
RewriteRule ^(.*) index.php?file=$1 [NC,L,QSA]

htaccess language rediretion

I have a website, www.thesite.com and an alias www.lesite.com
i need a htaccess redirection:
I would like that, when I go to www.thesite.com/fr, it redirects me to www.lesite.com/fr
Likewise, when I go to www.lesite.com/en, I would like it to redirect me to www.thesite.com/en
I have tried different methods but i only succeed to create infinite loops !----
Options +FollowSymlinks
RewriteRule ^fr$ http://dev.mariage.ch/fr/ [L]
RewriteRule ^de$ http://dev.hortzeit.ch/de/ [L]
OR
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch\/de\/
RewriteRule (.*) http://dev.hortzeit.ch/de$1 [R=301,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch\/fr\/
RewriteRule (.*) http://dev.mariage.ch/fr$1 [R=301,L]
You can't use path as part of RewriteCond for HTTP_HOST
instead of dev.hortzeit.ch/de you must use just host part dev.hortzeit.ch
RewriteEngine On
RewriteCond %{HTTP_HOST} !^dev\.hortzeit\.ch [NC]
RewriteRule ^de(/?.*)$ http://dev.hortzeit.ch/de$1 [R=301,NC,L]
RewriteCond %{HTTP_HOST} !^dev\.mariage\.ch [NC]
RewriteRule ^fr(/?.*)$ http://dev.mariage.ch/fr$1 [R=301,NC,L]

A better way to URL Rewrite this .htaccess

Could there be a better way of doing this I wonder. It is doing the following:
A) routing mydomain.uk to www.mydomain.uk
B) routing anything non http to https://www.mydomain.uk
I dont use linux based servers much so im sure there is a better way of doing this. End result should be any base url should end up at https://www.mydomain.uk
Heres the code
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule ^/?$ "https\:\/\/www\.mydomain\.uk\/" [R=301,L]
These 2 rules can be easily combined into one as this one:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.mydomain.uk%{REQUEST_URI} [L,R=301,NE]
You can use OR
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^mydomain.uk$
RewriteRule (.*) https://www.mydomain.uk%{REQUEST_URI} [R,L]
The rule RewriteCond %{HTTP_HOST} ^mydomain.uk$ can also be more restrictive and catch any host which is not www.mydomain.uk.

htaccess redirect to enother domain and language

I need to write a rewrite rule to redirect from one domain to another.
maindomain.com/en/ -> secounddomain.com/en/
maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
www.maindomain.com/en/ -> secounddomain.com/en/
www.maindomain.com/en/pagename.html -> secounddomain.com/en/pagename.html
secounddomain.com -> secounddomain.com/en/
www.secounddomain.com -> secounddomain.com/en/
and for secure (no back)
secounddomain.com/pl/ -> secounddomain.com/en/
www.secounddomain.com/pl/ -> secounddomain.com/en/
I try to do it like this but it doesn't work:
RewriteCond %{HTTP_HOST} ^maindomain.com/en/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^www.maindomain.com/en/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^www.secounddomain.com/pl/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
RewriteCond %{HTTP_HOST} ^secounddomain.com/pl/$ [NC]
RewriteRule ^(.*)$ http://www.secounddomain.com/en/$1 [r=301,L]
Best regards.
First of all this condition is wrong:
RewriteCond %{HTTP_HOST} ^maindomain.com/en/$
As %{HTTP_HOST} can only match host name hence it can match maindomain.com only.
Here is how your .htaccess should look like:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?maindomain\.com$ [NC]
RewriteRule ^(en(?:/.*|))$ http://secounddomain.com/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule (?!^en/)^(.*)$ http://secounddomain.com/en/$1 [L,R=301,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?secounddomain\.com$ [NC]
RewriteRule ^pl(/.*|)$ http://secounddomain.com/en$1 [L,R=301,NC]

.htaccess conditional statement

I want to rewrite my urls to more seo urls using wildcard sub-domains and I am unable to write htaccess conditions and hoping to get some help with it.
I have this url:
http://learntipsandtricks.com/blog/c/magento
I want to rewrite as:
http://magento.learntipsandtricks.com/
Change pagination link:
http://learntipsandtricks.com/blog/92/magento/3
to:
http://magento.learntipsandtricks.com/p-92-3
Finally change article url:
http://learntipsandtricks.com/blog/magento/114/magento-index-management-Cannot-initialize-the-indexer-process
to:
http://magento.learntipsandtricks.com/114-magento-index-management-Cannot-initialize-the-indexer-process
I wrote this:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/p\-(\d+)\-(d+) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com\/(\d+)\-(.*) [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/%2/%3/$1 [P]
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/c/%1/$1 [P]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php/$1 [L]
The above code doesn't work. Is it possible using if else in htaccess to check all types of urls and rewrite accordingly?
EDIT:
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%1/[first part of sub-domain]/%2/$1 [P]
How can I get first part of sub-domain here.
Thank you.
HTTP_HOST doesn't include the URL-path. If you want to match against the URL-path, use REQUEST_URI instead
RewriteCond %{REQUEST_URI} ^/p-(\d+)-(\d+)$
for example.
From RewriteCond Directive
RewriteCond backreferences: These are backreferences of the form %N (0 <= N <= 9). %1 to %9 provide access to the grouped parts (again, in parentheses) of the pattern, from the last matched RewriteCond in the current set of conditions. %0 provides access to the whole string matched by that pattern.
Therefore, if you want to capture both the domain and URL-path components, you must combine HTTP_HOST and REQUEST_URI in one RewriteCond
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteCond %{HTTP_HOST} (.+)\.learntipsandtricks\.com [NC]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^(.+)\.learntipsandtricks\.com/p-(\d+)-(\d+)$ [NC]
RewriteRule ^(.*)$ http://learntipsandtricks.com/blog/%2/%1/%3/$1 [P]

Resources