apache rewrite for certain urls to be http instead of https - apache

Further to an earlier question I had where I needed to rewrite urls such as
http://example.com/index.php?cPath=371_1659_1660&main_page=products_categories
to
http://example.com/index.php?cPath=371_1659_1660
I used this to fix it
RewriteCond %{QUERY_STRING} ^(.)(^|&)main_page=products_categories(.)$
RewriteRule ^(.*)$ /$1?%1%3 [R=301,L]
I've also discovered that I also have the same urls as https so
https://example.com/index.php?cPath=371_1659_1660&main_page=products_categories
and
http://example.com/index.php?cPath=371_1659_1660&main_page=products_categories
are giving me duplicate page penalties
Is it possible to modify my rewrite rule so that it also redirects to the http page at the same time as removing the
&main_page=products_categories
suffix so that
https://example.com/index.php?cPath=371_1659_1660&main_page=products_categories
would become
http://example.com/index.php?cPath=371_1659_1660

Not sure why this question was unfairly down voted.
You can use this single rule to redirect both URLs to http://...:
RewriteEngine On
RewriteCond %{QUERY_STRING} (?:^|&)main_page=products_categories(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)(cPath=[^&]+) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}?%1 [R=301,L,NE]

Related

https://example.com redirects to a wrong url using mod_rewrite

so I'm having trouble with .htaccess file. This is my code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
it redirects first from non www to www, and then from non https to https but it has a problem, when going to:
https://example.com
i.e: with https but not www it redirects me to a huge link, something like:
https://www.example.com/home/c3238912/public_html/httpss://www.example.com/home/c3238912/public_html/httpss://www.example.com/home/c3238912/public_html/httpss://www.example.com/home/c3238912/public_html/
How can be this possible?
Did you duplicate the s in your https redirect but forgot to mention it here?
Your example shows httpss in the redirected path. A typo like this would cause this issue as the httpss protocol doesn't exist, it's considered as a path, hence the infinite redirect.
My understanding is you real rule is:
RewriteRule ^(.*)$ httpss://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Remove the extra s, exactly like you show it in your question.
If you had a typo before, but corrected it, make sure you empty your cache. Redirects are cached.
It seems to me that it is running your second Redirect multiple times. Instead of having your rules split, you can connect them which should hopefully fix the multiple redirects.
Try using:
RewriteCond %{HTTPS} !=on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
I've changed the R flag to 302, this makes it a temporary redirect for testing purposes. If you find this rule works, then change it back to 301 for a permanent redirect.
Make sure you clear your cache before testing this.

Enforce HTTPS across thousands of 301 RewriteRule entries

community. I'm migrating an ancient ColdFusion site (seriously), and we have literally thousands of 301 redirects from old versions of pages like
index.cfm?id=1&this=that
to more reasonable counterparts like
/what-this-page-really-is
Google says to list them out one by one, so that's what I'm doing. However, on ALL of these redirects we also want to enforce HTTPS. What we have now is this:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^thing.cfm$ /thing/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah.cfm$ /awesome-sauce/? [R=301,L,NC]
It works fine, but Google complains that it's technically two redirects for these old links. A possible solution would be to exclude *.cfm files from the HTTPS redirect and explicitly make every single *.cfm redirect go to the HTTPS version of the page, like so:
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !(.*)cfm$
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^thing.cfm$ https://%{HTTP_HOST}/thing/? [R=301,L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah.cfm$ https://%{HTTP_HOST}/awesome-sauce/? [R=301,L,NC]
But over thousands of lines, that's a lot of extra characters in an .htaccess file. I know this is better in apache config, but I don't have that option right now.
My question: is there a way to set a flag that's going to apply the HTTPS parameter to ALL of the subsequent *.cfm 301 redirects?
Thank you!
Rather than enforcing https:// in each 301 handler you can move your http->https rule at the bottom and remove R flag from your redirect flags so that Google (or any external client) gets only one single redirect.
RewriteEngine On
RewriteRule ^thing\.cfm$ /thing/? [L,NC]
RewriteCond %{QUERY_STRING} id=1 [NC]
RewriteRule ^blah\.cfm$ /awesome-sauce/? [L,NC]
# all other rules here but remove R=301 flag
# finally do a http->https with rewritten URI
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L,NE]
Make sure to clear your browser cache before testing.

.htaccess applies only to root, while I need it to work across the domain name

I have the following ReWrite Rule
RewriteCond %{REQUEST_URI} !/maintenance.php$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteCond %{REMOTE_ADDR} !{developer_ip}
RewriteRule .* /maintenance.php [R=302,L]
This .htaccess should redirect all requests to the maintenance.php, except the requests coming from {developer_ip} which is my own ip address.
Problem:
The above rules work, but when I click on any of the internal links, it again shows me maintenance.php (which due to the IP rule, must not happen) which ends in either a index.php?{some_query} or a URL which is also already rewritten by the .htaccess itself, such as /Page/About-US (which originally is index.php?page_id=200.
Now I want the .htaccess to redirect all requests to maintenace.php (which already is doing) but no the requests coming from {developer_ip}. The above rules are fine, except the part excluding my own ip address, which redirects me for the internal links.
Perform an internal rewrite to maintenance page and keep your rules in this order:
RewriteEngine On
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif)$ [NC]
RewriteCond %{REMOTE_ADDR} !{developer_ip}
RewriteRule !^maintenance\.php$ /maintenance.php [NC,L]
# skip any requests alredy rewritten to maintenance.php
RewriteRule ^maintenance\.php$ - [NC,L]
# rest of your rewrite rules

Htaccess ReWrite URL with ? in duplicate URL's

We have moved from IIS to Apache and used the below rewrite rule to redirect www.example.co.uk/Default.aspx?pagename=About-Us to the new page url www.example.co.uk/About-Us/
RewriteCond %{REQUEST_URI} ^/Default\.aspx$
RewriteCond %{QUERY_STRING} ^pagename=About-Us$
RewriteRule ^(.*)$ http://www.domain.co.uk/About-Us/ [R=301,L]
The problem is this rule is showing up as duplicate content in google WMT and the page is being served at both URL's...
Can anyone spot what is wrong with this rule?
Change your rule to this:
RewriteCond %{QUERY_STRING} ^pagename=About-Us$ [NC]
RewriteRule ^Default\.aspx$ http://www.domain.co.uk/About-Us/? [R=301,L,NC]
Also note that it will take some time for search bots to remove duplicate URIs.

Apache mod rewrite rule issue

I am trying to write a mod_rewrite rule for my apache server. My requirement is that I have three web applications on a server, out of which all request to HTTP scheme should be redirected to HTTPS.
Here's what I have written :
RewriteEngine On
RewriteCond $1 ^abc$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
This doesn't seems to be working fine. I am trying to run application with abc context root to run on HTTP and all other requests to be redirected to HTTPS URL.
Can anyone tell me what am I doing wrong.
I see a couple of problems with your first rule:
The rule probably isn't matching, because the REQUEST_URI is /abc, not abc.
If the rule were matching, you'd have an infinite redirect loop, leading to a 500 Internal Server Error to the client, and a "Redirect limit exceeded" error in your logs. The problem is that the rule target is identical to the original request, so it will enter a redirect loop.
I suggest getting rid of the first rule, and adding the /abc exclusion to the second rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/abc$ [NC]
RewriteRule $ https://%{HTTP_HOST}%{REQUEST_URI} [L,R]
If you also want to force /abc to be on HTTP and not HTTPS, then you could add a second rule:
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/abc$ [NC]
RewriteRule $ http://%{HTTP_HOST}%{REQUEST_URI} [L,R]