My site dont have a http://, .htaccess not matching - apache

I am using http://htaccess.mwl.be/ to test out the htaccess file.
Rewrite link: http://mawek3.pro-linuxpl.com/de/test/
htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(en|fr|de)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
/test/ is just: echo $_GET['lang'];
The htaccess tester shows me that some rule is not meet if iam not using http:// at the address and I'm still getting lang=en value.
When i add http:// to website address(in httaccess tester) it works fine and displays "de".
The problem is that my webstie address don't display the http:// on the begining and rewriting method not working. What I'm doing wrong?

Related

Adding directory to url with htaccess & rewrite

I have a site based on Modx CMS and I need to create a 301 redirect for the main folder & old links after the multilanguage functionality has been enabled.
What I need to achieve is the default fallback to english eg. make sure that:
example.com/products -> example.com/en/products
example.com/about - example.com/en/about
etc.
I also need to make sure that if there already is a language selection (for example de) in the url, I don't add en to url. (so no example.com/en/de/products)
I am having trouble adding the /en/ to url and I am ending up with infite /en/en/en loops on the URL
To add the /en/ to the url I tried the following.
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
It results to a endless /en/en/en loop in the url.
The whole htaccess is as follow:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /
RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Try the code below. The rules are chained (the default is AND). So, if your URL doesn't start with /en, /de, it's not the index (/), and it's not a file name or a directory, do a 301 redirect to the English version:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

.htaccess https to http redirect conflict with existing rule

Currently we have this rule in .htaccess
RewriteEngine on <br>
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]
What this does is for any visit we will redirect to subfolder call site if the page not found. And when this happen the url will not show site as subfolder.
Example if we have
/rootFolder/site/temp.html this will show in url as
http://www.domain.com/temp.html
This is working fine but now we need to add https redirect if user visit site.
This is the new rule I came up with
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ https://%{HTTP_HOST}/site/$1
RewriteRule ^(/)?$ https://%{HTTP_HOST}/site/index [L]
But the issue with this one is now url will show site subfolder
https://www.domain.com/site/temp.html
How can I achieve so that if user does
http://www.domain.com/temp.html it will find temp.html in site subfolder and redirect to https and url will only show
https://www.domain.com/temp.html
Thanks
How about if you do it in two passes, always redirecting whatever was requested to https, then running your rewrite rules, like so:
RewriteCond %{HTTPS} off
#301 flag redirects instead of rewriting
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#leaving your original rules as is
RewriteCond %{REQUEST_URI} !^/site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/$1
RewriteRule ^(/)?$ site/index [L]

htaccess redirect to subfolder with a trailing slash

I successfully managed to redirect all requests to a subfolder of my web server.
This is the code I'm using:
/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1 [L]
/public/.htaccess
# redirect all urls that doesn’t have a
# trailing slash to urls with a trailing slash
# (this is my try)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1 [NC]
It works perfectly, so the following redirects work:
example.com -> example.com/public/
example.com/foo/ -> example.com/public/foo/
example.com/foo/about.html -> example.com/public/foo/about.html
By the way, the following redirect has a problem:
example.com/foo -> example.com/public/foo
Although it redirects correctly, the actual URL in the address bar is:
example.com/public/foo/
So my question is:
How can I have a clean URL when redirecting from a URL without a forward slash?
Hope it makes sense.
Place this code in root .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /public/$1/ [L]
Then just remove /public/.htaccess since that is not needed.

.htaccess - is rewriting one part of the url

Options +SymLinksIfOwnerMatch
IndexIgnore */*
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mysite.com$ [nc]
Rewriterule ^(.*)$ http://www.mysite.com/$1 [r=301,nc,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . url_handle.php
What I am trying to do is have a www rewrite rule so that urls without www. redirect to the same url with www. prefix. I am also using clean URL's, so any unknown URL is sent to url_handle.php for processing.
The problem is that if I have a url like "http://mysite.com/part_1/part_2/part_3", the URL is redirected and then reads "http://www.mysite.com/url_handle.php/part_2/part_3", ommitting the "part_1" and replacing it with "url_handle.php".
Firstly, to redirect non-www sites to www use this:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.mysite\.com [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) http://www.mysite.com/$1 [L,R,NE]
It means, if the url is not "www.mysite.com" and if not blank redirect to "www.mysite.com"
Secondly, to redirect unknown sites to a specific page use this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ http://www.mysite.com/url_handle.php [R=301,L]
The last row will save you :)