htaccess redirect double slash URL 443 - apache

I have a webpage that's reachable via HTTPS and I have some Urls with a double-slash (//) included. Those I want to replace with single-slash (/) or just rewrite/redirect to another page.
https://www.domain.com/us//whatever/page/
https://www.domain.com/us/whatever/page/
Normally I do redirects like this:
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule ^us/xyz/?$ https://www.domain.com/us/newpage/? [NC,L,R=301]
And double-slashes (//) in the RewriteRule don't work for for the htaccess redirects :/
I've already searched for a solution for replacing // with / and I found:
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
But when I use this snippet and I open the URL with the double-slash - this happens:
Opened url: https://www.domain.com/us//whatever/page/
Redirects to: http://www.domain.com:443/us/whatever/page/
Need help here. How can I fix this?
And is there an additional way to rewrite just a specific url that includes a double-slash (//)?
Thank you very much!

Related

URL rewriting and double slash after domain : how to make it single slash?

I want to write a rule to redirect (301) all pages from https://dev.example.com to https://www.example.com
On the root of dev.domain.com, on my .htaccess I have put this single line :
RewriteRule ^/(.*)$ https://www.example.com/$1 [R=301,L]
The redirection works but all subpages have a double slash.
Example : https://dev.example.com/contact.html is redirected to https://www.example.com//contact.html
Which is not working !
How can I solve this issue ?
Could you please try following, written and tested with your shown samples. Please clear your browser cache before testing your URLs. Also these Rules will look for URLs which are not having www in their HTTP_HOST variable part and then will remove domain name(very first part till first occurrence of .) with www and rest of the domain name's part.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?!www\.)(?:[^.]*\.)(.*)
RewriteRule ^(.*)$ https://www.%1%{REQUEST_URI} [NE,L]

apache redirect to remove a subfolder from the url

I have issues redirecting a specific url within the same domain
Example, I want to redirect
http://example.tld/folder/*
to
http://example.tld/*
where * is a wildcard
Basically you will need this type of code in your .htaccess file:
RewriteEngine On
RewriteRule ^/folder/ http://domain.tld/? [R=301,L]
This should redirect your current url http://domain.tld/folder to http:/domain.tld/
Or:
RewriteEngine On
RewriteRule ^folder/(.*)$ /$1 [R=301,NC,L]
Please try and see which one works best for you

%25 in being appended in htaccess redirected Url

i am tried o redirect all non https url to http so i am using this in .htaccess
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{SERVER_NAME}/$1 [B,R,L]
but when i remove https from url for testing , it redirect to https but it goes to https://example.com/%25myfile.php
that %25 is being added in url and making link NOT FOUND . am i making some mistake in above? I followed Apache: %25 in url (400 Bad Request) and added flag B as shown in above code but it not work .
Thanks .
Change your rule with this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [NE,R=301,L]
NE flag prevents encoding special characters.
%{REQUEST_URI} uses original URI not relative to current directory.
Make sure to clear your browser cache before testing the change.

Redirect URL (including port) with params using .htaccess

I am trying to redirect from
http://www.example.com:81/my/api/search?query=test
to
http://www.example.com:81/my/php/api.php?query=test
using
RewriteCond %{QUERY_STRING} ^query=(.*)$
RewriteRule ^api/search(.*)$ /php/api.php?%1 [L]
However, it doesn't work for me. Additionaly for testing htaccess rules I use http://htaccess.madewithlove.be/.
Possibly even better would be to check if request starts from ^api and it's GET type (I guess using RewriteCond %{REQUEST_METHOD}) then redirect to /my/php/api.php?[query params here]
Anyone can point me into right direction?
If your .htaccess is located in /my/ directory then you can use:
RewriteEngine On
RewriteBase /my/
RewriteCond %{QUERY_STRING} ^query=.
RewriteRule ^api/search/?$ php/api.php [L]
QUERY_STRING will be automatically carried over to target URL.

Redirect all pages in direcory except one subdirectory .htaccess

I want to have one URL still being served from a subdirectory on the old domain but redirect all others from the parent directory to the new domain.
www.olddomain.co.uk/directorya/directoryb -> no redirect
www.olddomain.co.uk/directorya -> www.newdomain.co.uk/directorya
This works if the URL is typed with a trailing slash
RewriteCond %{REQUEST_URI} !^/directorya\/directoryb [NC]
RewriteRule ^(.*)$ http://www.newdomain.co.uk/ [L,R=301]
So http://www.olddomain.co.uk/directorya/directoryb/ works but http://www.olddomain.co.uk/directorya/directoryb fails.
Tried added $ at the end and other connotations and tried
RewriteEngine on
RewriteRule !^myspecialdirectory/mynextdirectory($|/) http://newdomain.example%{REQUEST_URI} [L,R=301]
from Redirect entire site except one directory to a new site - apache .htaccess
What do I need to do to get http://www.olddomain.co.uk/directorya/directoryb/ or http://www.olddomain.co.uk/directorya/directoryb to redirect? (while all other http://www.olddomain.co.uk pages go to http://www.newdomain.co.uk
For reasons I don't understand http://www.olddomain.co.uk/directorya/directoryb goes to Google and doesn't redirect
Try using THE_REQUEST variable and make sure this this very first rule:
RewriteCond %{THE_REQUEST} !\s+/+directorya/directoryb [NC]
RewriteRule ^ http://www.newdomain.co.uk/? [L,R=301]
Test it after clearing your browser cache.