Redirect domains with subfolders drops slash - .htaccess

I do want to add www to all requested domains. This works fine so far as long as I don’t use subfolders.
Example: http://domain.fr redirects to http://www.domain.fr but http://domain.fr/panel drops the slash, redirecting to a wrong url http://domain.frpanel
Here is my rule:
RewriteCond %{HTTP_HOST} !^www\.domain\.fr$ [NC]
RewriteRule ^(.*)$ https://www.domain.fr$1 [R=301,L]
Thanks for helping me
René

Try with below,
RewriteCond %{HTTP_HOST} !^www\.domain\.fr$ [NC]
RewriteRule ^(.*)/?$ https://www.domain.fr/$1 [R=301,L]

Related

.htaccess : Redirect all sites on my joomla website to root/main domain except one

So I really don't know what I'm doing wrong here, but I'm not too familiar with .htaccess stuff. What I want to do is to redirect every sub-site on my webspace to the main/root domain, except for one specific page which would be " my-website.de/impressum-datenschutz ".
This is what I got
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/impressum-datenschutz$
RewriteRule ^(.*)$ http://my-website.de/ [R=301,L]
While the first part, redirecting from www to non-www works just fine, when I insert the second part, i get an error in my webbrowser telling me " this website redirects the request in a way so that it can never be finished" (roughly translated from German) .
What am I doing wrong?
Really thankful for any kind of help
For non-www to www use this
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/ [L,R=301]
For redirect all subdomains to domain try this
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://yourdomain.com/$1 [L,R=301]

Using ISAPI-Rewrite htaccess to 301 redirect multiple urls for multiple hostnames?

I have a single site within IIS, which runs on 4 different hostnames. The CMS handles that, and displays the correct site based on the incoming hostname.
I need to use ISAPI rewrite to handle all the old urls and 301 redirect them to the new equivalants, this is how I currently deal with redirects in my ISAPI-rewrite .htaccess file
RewriteEngine on
RewriteRule ^post/my-old-page-one$ /my-newer-page-one [R=301]
RewriteRule ^post/my-old-page-two$ /my-newer-page-two [R=301]
My issue is, I need it to check the domain too for the incoming url. As the sites have the same old urls, which now need to go to a different page.
I was hoping I could so this.
RewriteEngine on
RewriteRule ^http://www.siteone.com/post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301]
RewriteRule ^http://www.siteone.com/post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301]
RewriteRule ^http://www.sitetwo.com/post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301]
But it doesn't work. Any advice greatly appreciated.
The check of the hostname is performed in a separate condition, like this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.siteone.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.siteone\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.siteone.com/my-newer-page-two [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-one$ http://www.sitetwo.com/my-newer-page-one [R=301,NC,L]
RewriteCond %{HTTP_HOST} ^www\.sitetwo\.com$ [NC]
RewriteRule ^post/my-old-page-two$ http://www.sitetwo.com/my-newer-page-two [R=301,NC,L]

.htaccess mod_rewrite redirection not working

I am using the following condition/rule on a .htaccess file located at the root of my domain. The purpose is to redirect all non-www requests to their www. version:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
It seems to be working fine.
Inside my /blog/ subdirectory I have another .htaccess that I use to redirect fancy URLs to real ones:
RewriteCond %{THE_REQUEST} !.*?url=.*
RewriteRule (.*) index.php?url=$1 [QSA]
This also seems to be working fine. However, all non-www requests inside the /blog/ subdirectory are not being redirected to their www. version.
For example, if I type domain.com on the browser I correctly get redirected to www.domain.com. But if I type domain.com/blog/ or domain.com/blog/test-page/ I won't get redirected to the www. version.
Probably the .htaccess inside /blog/ is conflicting with the one at root level, but I don't know how or how to fix it. Any clues?
Update: I solved the problem by putting all the rules on the root .htaccess file. I had to tweak the fancy URL rules slightly to only catch the /blog/... requests. Here's the final .htaccess file in case it might help you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^apprush\.com$ [NC]
RewriteRule ^(.*)$ http://www.apprush.com/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/blog
RewriteCond %{REQUEST_URI} !.*?url=.*
RewriteRule blog/(.*) blog/index.php?url=$1 [QSA,L]
Are you able to put all of the rewrite rules into the root level .htaccess? It not only gets around the problem you're having but is a little neater because you know exactly where all of your rules are located.
If you are, these rules will do what you need:
RewriteCond ${HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
RewriteCond ${THE_REQUEST} !/blog/.*?url=.*
RewriteRule (.*) index.php?url=$1
This is because you have dollar sign $ at the end of domain.com. Also you need to use REQUEST_URI instead of HTTP_HOST. Remove it:
RewriteCond %{REQUEST_URI} ^domain\.com [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
The dollar sign signifies the end of the string, thus making the rewrite to work for direct requests to domain.com. The update script solves the problem.

Redirect subdomain to main domain except one url

Hi I have a main domain as follows
Main domain : http://www.sample.com/
Sub domain : http://dev.sample.com/
Now If any one access http://dev.sample.com/ It needs to redirect to http://www.sample.com/
So I added the following code in my sub domain It works very well
RewriteCond %{HTTP_HOST} !www.sample.com.com$ [NC]
RewriteRule ^(.*)$ http://www.sample.com.com/$1 [L,R=301]
But from subdomain if any access the following url http://dev.sample.com/data then
it should not redirect to main domain. It should stay on that sub domain page. Any one have idea?
Try these rules :
RewriteCond %{HTTP_HOST} ^dev\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/?data
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,QSA,R=301]
Try this:
RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/data [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
However, this will prevent all URL's with /data at the start of the URI. So if you had http://blog.sample.com/data it would also allow it to resolve without the rewrite. If this is a problem you can get more complex... after this rule add this to catch all non-dev subdomains:
RewriteCond %{HTTP_HOST} !dev\.sample\.com$ [NC]
RewriteCond %{HTTP_HOST} !www\.sample\.com$ [NC]
RewriteRule ^(.*)$ http://www.sample.com/$1 [L,R=301]
Now if the first condition set is not met the first rule won't run, so check to make sure you're only serving dev and www up with /data.

.htaccess redirect mainpage only

I need to redirect the main page only using htaccess as i've other redirects in the other folders.
I've tried
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [OR]
Rewritecond %{http_host} ^example.com [nc]
Rewriterule ^(.*)$ http://www.test.com/home/$1 [r=301,nc]
but it redirects the entire site and causes the rest of the redirects in the proceeding directories to not work.
Any help is truly appreciated.
Your rule ^(.*)$ means that any URL is redirected to http://www.test.com/home/$1 ; I think you just want to redirect /home/* and not the other directories, is it that ?
In that case, I think you simply need to change your rule to /home and not .*
Otherwise please give us some more information, 'cause it would mean I didn't understand your problem
RewriteEngine On
RewriteCond %{HTTP_HOST} ^expertshouse\.se?$ [NC]
RewriteRule ^(|/|/index.php)$ http://expertshouse.wix.com/home/ [R=301,L]

Resources