dear community.
Today i made this .htaccess rewrite rules ( found some part on the internet and added some by myself )
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
When i request some.site.ru - site.ru/some.php content triggered. It's fine. And when i request some.site.ru/readme.txt i see site.ru/readme.txt content. And i dont quite understand why its working fine.
I thought RewriteRule ^ %1.php makes all requests to go through *.php files. Am i wrong?
Additionally i cant understand RewriteRule ^ this part. Is it the same as RewriteRule ^(.*)$ ?
This is your rule:
RewriteCond %{HTTP_HOST} !^www.* [NC]
RewriteCond %{HTTP_HOST} ^(.*).site.com/(.*)$
RewriteRule ^ %1.php
Due to presence of faulty pattern in front of RewriteCond %{HTTP_HOST} it is a DO NOTHING rule, in other words this rule will never execute because of presence of a / in 2nd condition which can never be true because %{HTTP_HOST} matches only the host name part of a web request. So %{HTTP_HOST} can only match www.example.ru or example.ru.
So it is obvious that /readme.txt URL is working fine without any rewrite.
Related
I am trying to redirect my language variables domain.com?lang=X to subdomains X.domain.com
I have this code:
RewriteCond %{QUERY_STRING} (^|&)lang=(es|de|fi|tr)
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI} [R=301,L]
Unfortunately, when I want to change language by going to domain.com/page?lang=es, I'm redirected to es.domain.com/page?lang=es which causes a loop.
So I tried adding a questionmark to the end of the URL like so:
RewriteRule ^(.*)$ https://%2.domain.com%{REQUEST_URI}? [R=301,L]
to prevent it from adding the ?lang= variable when accessed by the subdomain but in that case the language doesn't change and I end up with es.domain.com/page in English.
I've spent the last 4 hours digging through StackOverflow and other sites trying to find a solution but to no avail. How can I detect that I'm already redirected to the subdomain and then ignore the ?lang= variable while actually changing the language?
Please help.
Thank you.
You will need 2 rules for this:
RewriteEngine On
# external redirect rule using THE_REQUEST variable
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteCond %{THE_REQUEST} \s/+([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com/%1? [NE,R=301,L]
# internal rewrite rule to add lang parameter back
RewriteCond %{QUERY_STRING} !(^|&)lang= [NC]
RewriteCond %{HTTP_HOST} ^(es|de|fi|tr)\. [NC]
RewriteRule ^ %{REQUEST_URI}?lang=%1 [L,QSA]
Could you please try following .htaccess Rules at the top of your htaccess file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
##use THE_REQUEST variable to match condition here, with NC flag.
RewriteCond %{THE_REQUEST} \s/([^?]*)\?&?lang=(es|de|fi|tr)\s [NC]
RewriteRule ^ https://%2.domain.com%1 [NE,R=301,L]
I have a set of URLs that are in the form www.website.co.uk/businessname
and these are meant to do a htaccess Proxy redirect to pull data from another domain on the server.
This is what I have:
#Start Business pages
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputside.org.uk/swellcottage$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn-books$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
#End Business Pages
The first rule works perfectly, displaying the contents of http://www.datasite.co.uk/swellcottage on the URL www.outputsite.org.uk/swellcottage but the second rule/condition doesn't work.
I looked at escaping the - character in the URL as so:
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn\-books$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
#End Business Pages
But this still doesn't seem to engage correctly, the rewrite rules do not work and the site replies with the fallback 404 that the page www.outputsite.org.uk/hawthorn-books does not exist.
Where else and how else should I escape the dash or otherwise what's wrong with my syntax here? cheers
ps. I have it that filenames (index.php) and folder names (/places/) are not subject to the proxy redirects .
UPDATE
I have more Rules in my htaccess:
#to add www. to each request.
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]
#Start biz pages
...
#End biz Pages
#to prevent error looping
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
I have also run the same code above with hawthornbooks without the dash, escaped or otherwise and the code works correctly, so I need to find out the correct way of escaping the dash character. All code and work is UTF-8.
NOTE: This issue appears similar to Why if I put a "-" dash in a rule in my htaccess doesn't work? but I found no solution to that problem.
If problem is just a hyphen this should work:
#Start Business pages
RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(swellcottage)/?$ http://www.datasite.co.uk/$1 [P,NC]
RewriteCond %{HTTP_HOST} ^(www\.)?outputside\.org\.uk$ [NC]
RewriteRule ^(hawthorn.books)/?$ http://www.datasite.co.uk/$1 [P,NC]
#End Business Pages
This is intended to be a temporary rather than a permanent solution, but you might try the following rule:
RewriteCond %{HTTP_HOST}%{REQUEST_URI} ^www.outputsite.org.uk/hawthorn([^\/]*)\/?$
RewriteRule ^(.*) http://www.datasite.co.uk/$1 [P]
This will look for any URI which conforms to the pattern www.outputsite.org.uk/hawthorn followed by any number of characters (or zero characters) which aren't / and then ending with an (optional) /.
Here is what I need to redirect to a temporary HTML page:
http://www.domain1.com/?Itemid=230
should get redirected to:
http://www.domain2.com/temoporary-solution.html
Here is what I came up with, just not sure if it will cause any issues between the rest of the .htaccess rules (this is the first rule):
RewriteCond %{HTTP_HOST} ^www\.domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html [R=302,NE,NC,L]
Your rule should work fine. Just append ? at the end of target URI to strip off existing query string:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain1\.com$ [NC]
RewriteCond %{QUERY_STRING} ^Itemid=230$ [NC]
RewriteRule ^$ http://domain2.com/temoporary-solution.html? [R=302,L]
Those rules are fine. The conditions are pretty strict so as long as it's the first rule, it won't break anything else.
I am trying to create a mod_rewrite rule to direct people to a sub-folder. Currently the code looks as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
The redirect works if the user types www.abcsite.com, but not if they type abc.com. Is there something that I am missing or should do differently to make sure the user goes to the correct folder (regardless of how they type the URL)?
Side note: The htaccess file that I am dealing with is a Joomla file, so all contents of it deal with another Joomla site. I appreciate the help.
Because you have conditions for that.
RewriteCond %{HTTP_HOST} abcsite.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abcsite\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
All above rules will pass only its abcsite.com
You add following rules also then it work for abc.com too.
RewriteCond %{HTTP_HOST} abc.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^!www\.abc\.*$
RewriteCond %{REQUEST_URI} !^/abc/.*$
RewriteRule (.*)$ /abc/$1 [L]
There's a stray ! in your second condition. A ! in front of the pattern means that the condition is true when the regex doesn't match (like in the third condition). A ! inside the pattern is just a literal symbol.
The host conditions should be something like:
RewriteCond %{HTTP_HOST} ^abcsite\.com$ [OR,NC]
RewriteCond %{HTTP_HOST} ^www\.abcsite\.com$ [NC]
And in fact, they can be joined into a single condition (note, no [OR] here):
RewriteCond %{HTTP_HOST} ^(www\.)?abcsite\.com$ [NC]
Your third condition is intended to prevent redirect loops (/foo → /abc/foo → /abc/abc/foo → …). What it says is that the rule isn't applied if the request URL starts with /abc/. However, your actual redirect is an internal redirect: if a user accesses abcsite.com/foo, the server internally rewrites this to /webroot/abc/foo, but REQUEST_URI stays the same, /foo.
The reason this doesn't cause a redirect loop as it is is likely rewrite rules in abc/.htaccess which override this one once the redirect is done.
What should be checked instead in the third condition is the path matched by the rewrite rule:
RewriteCond $1 !^abc/
RewriteRule (.*) /abc/$1 [L]
I need to have :
http://www.example.com/v1/my-project/ redirected to http://example.com/my-project/
so :
(1) remove the www from the http_host
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
(2) remove the 'v1/' part of the request_uri
RewriteCond %{REQUEST_URI} ^/v1/(.*)$ [NC]
RewriteRule . %1 [R=301,L]
(3) I also want to redirect all 404 to the homepage.
ErrorDocument 404 /
(4) Finally, all my documents actually reside in a "v2/" folder which hosts the current active website, but i don't want "v2" in the url, just "/"
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule ^(.*)$ /v2/$1 [NC,L]
So, here are my rules. My question is: i don't manage (2): it gets redirected to / (because of rule (3) i guess. I think the order of my rules must be faulty but i can't seem to get it right. Can you help ?
"Rule 3" isn't a rule at all, and its order relative to your RewriteRules doesn't matter. Rule 2 is failing for some other reason. I'm not sure whether it will address your problem, but I would simplify your rules somewhat by writing them like this:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteRule ^v1/(.*) /$1 [R=301,L,NC]
RewriteCond %{REQUEST_URI} !^/v2/ [NC]
RewriteRule (.*) /v2/$1 [NC,L]
You should first write any rule that is causing an external redirect (R flag) and then the other rules. Otherwise an already rewritten URL can be used for an external redirect though it was just intended for an internal redirect.
So I won’t change the order you have right now.