Rewriting from https to http with htaccess how to remove port number? - apache

Im having this htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} on [AND]
RewriteCond %{REQUEST_URI} /products-page/bathroom-amenities/nourish-a-spa-line/
RewriteRule (.*) http://%{HTTP_HOST}/bathroom-amenities/286-nourish.html
</IfModule>
what this does is to redirect the user from:
https://www.mydomain.com/products-page/bathroom-amenities/nourish-a-spa-line/
to:
http://www.mydomain.com:443/bathroom-amenities/286-nourish.html
This Code is redirecting from a https URL to a http URL, but for this to work I need to remove the port from the result URL.
Can anyone help me with this?

User SERVER_NAME instead of HTTP_HOST
Be careful with the use of UseCanonicalName setting, most likely you want to have it set to Off

Related

Htaccess rewrite rule redirected but server not processh

I want my server to read this page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
I have tried this .htaccess rule:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/myapp\.php$
RewriteCond %{QUERY_STRING} ^p=11
RewriteRule ^(.*)$ example.com/myapp/my-technical-effort [QSD,R=301,L]
it redirects to the new URL but the server says "page not found" on this server with 404 error.
Can you please help me to rewrite the rule so the server can serve the page example.com/myapp.php?p=11 against the URL example.com/myapp/my-technical-effort.
My current .htaccess is as follows:
<IfModule mod_headers.c>
Header always append X-Frame-Options ALLOWALL
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
</IfModule>
Anyone can help me please?
Your implementation is the wrong way round. Have a try that way:
RewriteEngine on
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
You need to implement your rewrite rules based on actual imcoming requests. Those should get rewritten internally, so those have to be matched.
This asumes that the actual request you want to rewrite is to /myapp/my-technical-effort (your question is a bit vague in that), and that /myapp.php?p=11 is the internal resource you want to get queried.
UPDATE:
Your comment below suggests that you are actually looking for a solution to redirect the "old" URL to the new one. It is indeed possible to combine that with above rewriting:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p=11$
RewriteRule ^/myapp\.php$ /myapp/my-technical-effort [QSD,R=301,L]
RewriteRule ^/?myapp/my-technical-effort$ /myapp.php?p=11 [QSD,L]
This will redirect any client that requests the "old" url (https://example.com/myapp.php?p=11) to the "new" url (https://example.com//myapp/my-technical-effort). And it will internally rewrite incoming requests to the "new" url to the actual script able to respond to the requests (/myapp.php).

Redirect http to https exclude specific IP

I have used the following rule to exclude 200.200.0.17 to be redirected from http to https, but its requests (from 200.200.0.17) are still being redirected to https. Any help will be appreciated.
<VirtualHost _default_:80>
# If mod_rewrite is present, it takes precedence over mod_alias
# and it is necessary to rewrite the request to https.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !^200.200.0.17
RewriteRule (.*) https://%{HTTP_HOST}$1
</IfModule>
# Otherwise use mod_alias to redirect.
Redirect / https://%{HTTP_HOST}/
</VirtualHost>
After the first rule is executed, the second one is also executed. To prevent this from happening, use the last flag. Change the line in your code:
RewriteRule (.*) https://%{HTTP_HOST}$1 [L]
See: https://httpd.apache.org/docs/2.4/rewrite/flags.htm
It's because you have the alternative redirect set up, remove:
Redirect / https://%{HTTP_HOST}/
Or it will just keep redirecting that IP after the first rule ignores it.

Redirect all HTTP to HTTPS

I have a SSL on my site and would like to redirect all my http pages to https
I find something below and work for www.yourdomain.com.
If I also need transfer all yourdomain.com(without www) to https what should I add to htaccess? Thanks!
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.yourdomain.com/$1 [R,L]
<!-- Please put the redirect without www here, thanks-->
A simple Google search reveals hundreds of results. For example, this official FAQ.
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context

Rewrite to a new domain? with url query? not working. :( help

I have this rewrite rule...
Redirect all initial request from world.example.com to web.example.com
RewriteCond %{HTTP_HOST} ^world\.example\.com$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{SERVER_PORT} !^80$
RewriteRule ^(.*)$ https://web.example.com$1 [R=301,L]
Which works great. But, some of my applications has...
https://world.example.com/approvals/?id=b47b5256567
Unfortunately, it is not being redirected properly to web.example.com. Instead, it just goes to web.example.com without the query params.
How could I make all request redirect properly to web.example.com along with the query params?
Basically, what it should do...
https://world.example.com/approvals/?id=b47b5256567
then
https://web.example.com/approvals/?id=b47b5256567
Just changing the world to web and passing the query string.
Help
You don't need to use the complicated rewrite engine to do this. Just use Redirect:
<VirtualHost *>
ServerName world.example.com
Redirect permanent / https://web.example.com/
</VirtualHost>
The Redirect directive automatically preserves everything that comes after what you're redirecting.
You forgot to use the "Query String Append" flag [R=301,L,QSA].
We can do it with Nginx, too.
server {
listen 80:
server_name: world.example.com
# URL redirect
#rewrite ^ http://web.example.com$request_uri? permanent;
rewrite ^ $scheme://web.example.com$request_uri permanent;
#rewrite ^ $scheme://web.example.com permanent;
#rewrite ^ $scheme://web.example.com;
}
Reference:
Nginx Redirect (Rewrite) Old Domain To New Domain With HTTP 301

Redirect using .htaccess

I want to redirect all user page requests to a page on the same domain.
For example, I have an "under construction, BRB" page that I want all users to see when they try to access ANY page on the site.
I tried using this:
Redirect 302 / http://www.domain.com/index2.php
What that does is try to apply the redirect to the index2.php page as well and it gets stuck in a loop where the user then sees this until the browser stops.
http://www.domain.com/index2.phpindex2.phpindex2.phpindex2.php etc., etc,
Any idea on how to write that rule to except that page?
You have to exclude the file you want to redirect to. Here’s an example with mod_rewrite:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule !^index2\.php$ /index2.php [L,R=302]
</IfModule>
To prevent endless looping (index2.php <--> index2.php):
Only redirect If URL to be redirected does NOT contain the string 'index2.php'
RewriteCond %{QUERY_STRING} !index2.php
RewriteRule ^(.*)$ /index2.php [L,R=301]
You could use mod_rewrite
<IfModule mod_rewrite.c>
RewriteCond {REQUEST_URI} !=/index2.php
RewriteEngine on
RewriteRule ^.*$ /index2.php
# End .HTACCESS
</IfModule>
I'd be more inclined to use the slightly different
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/.*$ /index2.php [R=307,L]
</IfModule>
This will let you return a moved temporarily status for the redirect.
Omitting this set of flags means that mod_rewrite will return a 302 Found status by default.
HTH
cheers,