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.
Related
I have here a little problem with a website and its WordPress blog.
For a short time, we had setup everything with https, until we were facing some issues and had to go back to HTTP.
Back then, I had a little collection of .htaccess files to deal with these kinds of problems, but I never actually tried my "non-www to www - ssl"
The intent was to add www and redirect https to http
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/ [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Seemed pretty simple to me and I thought it should work.
I have two .htaccess files, one for http://www.example.com/ and one for http://www.example.com/blog both with the same content, as users are primarily coming from SE's via Blog.
The Problem is: If I load https://www.example.com/blog I get redirected to http://www.example.com/ instead of http://www.example.com/blog.
While writing the Question I thought I try this Question I had the idea to add this
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
but then I get only redirected to http://www.example.com/{REQUEST_URI}
Could someone please tell me how I can keep the path on that redirect query?
RewriteCond %{HTTPS} on
RewriteRule ^/?$ http://%{SERVER_NAME}/{REQUEST_URI} [R=301,L]
If you notice that in your other directive you have %{REQUEST_URI}. You are missing the % prefix above. This is required syntax in order to get the value of the REQUEST_URI server variable. But also note that the value of REQUEST_URI already includes a slash prefix, so the slash should be omitted from the substitution. ie. instead of /{REQUEST_URI} it should be %{REQUEST_URI}.
Also note that the RewriteRule pattern (^/?$) only matches the root of your site (or /blog subdirectory if this .htaccess file is in that subdirectory). You need to match everything. So, modify the above RewriteRule like this:
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]
I've also hardcoded the domain, otherwise, you'll end up with a double redirect when requesting the non-canonical (ie. non-www) host.
I have a situation where I need to force every single page in my site to redirect to HTTP except for two specific URLs which need to force redirect to HTTPS.
The two pages that need to redirect to HTTPS pages are:
/microsoft-moc-on-demand-video-training/moc-registration-page/
/courses/register/
The code I've been using in my .htaccess file looks like this:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(/courses/register/|/microsoft-moc-on-demand-video-training/moc-registration-page/)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Unfortunately this doesn't seem to be working. The entire site does redirect to HTTP (so part of the code works), but those two exceptions (which should redirect to HTTPS) do not do that, they stay as HTTP links.
Any idea what I'm doing wrong here?
The problem is that for RewriteRule, there is no initial / in the requested path. Therefore, you're trying to match something that isn't present.
You also had an extra / at the end of each option in the first capture group, which when combined with the final / would require a path such as /courses/register//.
The following code should suit your needs:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/courses/register/
RewriteCond %{REQUEST_URI} !^/microsoft-moc-on-demand-video-training/moc-registration-page/
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(courses/register|microsoft-moc-on-demand-video-training/moc-registration-page)/ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I have multiple subdomains that are working with a redirect, but bad for SEO
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.actions.mydomain.nl [R=301,L]
I want to redirecting to:
http://$1.mydomain.nl/actions
but here i get a 404 error.
How to make the right rule for this?
I assume that your rule looks like this:
RewriteRule ^([a-zA-Z0-9&:_-]+)/actions$ http://$1.mydomain.nl/actions [R=301,L]
So the destination becomes http://subdomain-name.mydomain.nl/actions. So the first thing you need is to make sure a DNS entry is setup to point subdomain-name.mydomain.nl to the right IP address/server. Then on the actual server you need to make sure that the requests are routed to the right place. So assuming that the actual resource is /subdomain-name/actions, you'd need:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.mydomain.nl$ [NC]
RewriteRule ^actions$ /%1/actions [L]
This is the complete code i have:
RewriteCond %{HTTP_HOST} !^www.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
RewriteRule ^$ file.php?seo_naam=%1 [L,QSA]
This is working,but multiple(2 level) subdomains are not a good thing for seo.
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).actions.mydomain.nl$ [NC]
but i want to change this to:
RewriteCond %{HTTP_HOST} ^([a-zA-Z0-9&:_-]+).mydomain.nl\actions$ [NC]
does not work
the thing is that the file.php?seo_naam=%1 should be loaded otherwise it will not work.
I want to have a multi language site. Now, I have 2 domains. The first one is the main domain. That is website.nl. And i have a domain alias, website.org. So the 2 domains share the same public_html folder.
What I want is that:
website.nl will use the file /index.php/$1 and
website.org will use the file /gb/index.php/$1 (So when the url is website.org/test you will use the file /gb/index.php/test (No url redirect)
I found on another topic on stackoverflow the following:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} website.org
RewriteRule ^(.*)$ /gb/index.php [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
But this htaccess file won't work. I will get a 500 error. That's all.
Can someone see what's going wrong?
Your rules are looping, otherwise the 2 rules will mess with each other and loop indefinitely (e.g. requesting /foo will result in /index.php/index.php/index.php/index.php... etc thus returning 500). You need to add some conditions to stop the looping. Try changing the conditions and rules to:
RewriteCond %{HTTP_HOST} website.org
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteRule ^(.*)$ /gb/index.php/$1 [L]
RewriteCond %{REQUEST_URI} !^/gb/index.php
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php/$1 [L]
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]