How to force htaccess to remove last dash (-} - .htaccess

What I need:
force to HTTPS, remove WWW and remove last dash (-)
https://example.net/post/post-name-
I need it go to
https://example.net/post/post-name
I already have a Rewrite Rule that removes WWW and forces redirect to HTTPS. But if I do
RewriteRule ^(.*)-$ https://example.net/$1 [R=301,L]
the rule to remove WWW don't work.
Can you help me?

You can use this :
RewriteEngine on
#force ssl and non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.net$ [NC]
RewriteRule (.*) https://example.net%{REQUEST_URI} [NE,L,R=301]
#remove trailing hyphen "-" from uris
RewriteRule ^(.*)-$ /$1 [L,R=301]

Related

301 Redirect www and other subdomains in a subdirectory to non-www

301 Redirect by htaccess all URLs with any subdomains in a subdirectory to non-www
Examples:
https://www.example.com/forum
to https://example.com/forum
https://anysubdomains.example.com/forum
to https://example.com/forum
https://www.example.com/forum/anysubdiretory
to https://example.com/forum/anysubdirectory
https://anydubdomains.example.com/forum/anysubdiretory
to https://example.com/forum/anysubdirectory
The code below works only for www but how to make it work for all subdomains:
RewriteEngine on
#the directory the rule should apply to
RewriteCond %{REQUEST_URI} ^/forum/ [NC]
#check if the host string starts with "www"
RewriteCond %{HTTP_HOST} ^www\. [NC]
#redirect all www urls to non-www
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301]
To match any subdomains including www , you can use a regex pattern that matches everything . Your RewruteCondition currently only matches a www subdomain , replace it with a wildcard match RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC] .
You can use the following :
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/forum
RewriteCond %{HTTP_HOST} ^(.+)\.example\.com$ [NC]
RewriteRule (.*) https://example.com%{REQUEST_URI} [L,R=301]
Make sure to clear your browser cache before testing this new redirect.

How to always redirect to https://www and https://subdomain?

I want to redirect everything to URLs that start with https://www or https://subdomain. If it's a URL with no subdomain then I want it to always redirect to the https://www version. And if there is a non-www subdomain then I want it to always redirect to the https://subdomain version.
The first thing I had would just redirect everything to https://www. Here's that:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
I think the only problem with that is that it will redirect non-www subdomains to the multilevel https://www.subdomain. So then I added a second condition to the second rule:
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} !^(.*)\.(.*)\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
This appears to work for everything I want with one problem. If there is a subdomain and something after the domain like a directory then it won't work. For example this:
sub.site.com
will correctly redirect to:
https://sub.site.com
But this:
sub.site.com/test/
will not redirect to:
https://sub.site.com/test/
So why doesn't this last URL get rewritten when there is something after the domain like the /test/ directory?

Redirect from non-www to www and force SSL

I'd like to force all http traffic to https, and also always force www.
This is what I have so far:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
This seems to correctly work when the url does not contain www. So http://mydomain.com correctly redirects to https://www.mydomain.com.
However, it is not correctly redirecting to https when the www part is present. So, www.mydomain.com is not redirecting to https://www.mydomain.com
Edit
I have got this working with two rewrite blocks:
# Force ssl
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# redirect non-www to www
RewriteCond %{HTTP_HOST} ^mydomain\.com
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
No idea if this is acceptable or not, but it works
Use your domain name and not HTTP_HOST in the rewriterule so that way it won't matter if www is there or not and you can use 1 rule. You can have two rewrite rules but just use OR. I think you were looking for is this.
# always redirect to www and/or https
RewriteCond %{HTTP_HOST} ^mydomain\.com [NC,OR]
RewriteCond %{HTTPS} !^on$
RewriteRule ^(.*) https://www.mydomain.com/$1 [R=301,L]

Rewriting Subdomains to Subdirectory without redirection

I want to create virtual subdomains
like:
user1.domain.com/anythging >> domain.com/users/user1/anything
Without redirection
I use this cdoe in htaccess:
RewriteEngine on
DirectorySlash off
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com.*$ [NC]
RewriteRule ^(.*)$ http://domain.com/users/%1/$1 [P,L,NC,QSA]
But when I use :
DirectorySlash off
I can rewrite:
user1.domain.com/anythging/ >> domain.com/users/user1/anything
with slash (/) at the end ( anythging/ ),
But this
user1.domain.com/anythging >> error 403: Forbidden
You don't have permission to access /users/user1/anythging on this server.
in the second case, when I use:
DirectorySlash ON
this:
user1.domain.com/anythging >> redirects to >> domain.com/users/user1/anythging
But I whant to rewrite the link not redirect it!
Any idea where is the problem?
Not sure why DirectorySlash isn't working for you but you don't exactly need it to achieve what you want. This .htaccess would also work. I'm assuming you want any trailing slash if present to be dropped.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1 [L]
Also, there's no need for proxy [P], query string append [QSA] (it's on by default) and no-case [NC] (since, the rewrite regex only contains wildcards).
I found the solution:
RewriteEngine on
DirectorySlash off
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.([^\.]*)\.domain\.com$ [NC]
RewriteRule (.*) http://%1.domain.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteRule ^(.*?)/?$ http://domain.com/users/%1/$1/ [P]
it might need some cleaning to redirect all www links to non-www and to exclude the www.domain.com from subdomain rewriting

How to add subdomain in .htaccess and force WWW in url

I have the following code to force WWW in url
rewriteCond %{HTTP_HOST} !^www.example.com [NC]
rewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
I want to add a subdomain max.example and refer to www.example.com/update/max directory
If I do not add any rule, max.example.com will redirect to www.example.com/update/max . If I remove force WWW in url it works. But I still need force WWW in url. How to write the rule?
Thanks!
Change your rule to this:
RewriteCond %{HTTP_HOST} !^(www|max)\.example\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [R=301,L]

Resources