.htaccess is concatenating rules - apache

I have a .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
this is working for http://www.domain.com/frases/13571/
The point is that I need to redirect all HTTP to HTTPS after this rule is already applied.
So I write this:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^([a-zA-Z-]+)\/([0-9]*)\/?$ ler.php?categoria=$1&id=$2
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But isn't working like I supposed to. I'm getting this when I perform a HTTP request:
https://www.domain.com/frases/13571/?categoria=frases&id=13571
What is the right code that just change the protocols without modify the others parts of url?
I think that my code is doing 2 requests in the first rules and that is generating this wrong url.

L flag must be applied to your rewrite rule.
You can use this code instead
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^([^/]+)/([^/]+)/$ /ler.php?categoria=$1&id=$2 [L]
Note:
you don't need to escape slashes (\/ can be /)
you'll need to clear your browser's cache before trying again with this code. Your old rule is now in cache and you won't see the new code working if you don't clear it.

RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You rewrite from the original REQUEST_URI, the current is what you have captured, so try:
RewriteRule (.*) https://%{HTTP_HOST}$1 [R=301,L]
or
RewriteRule (.*) https://$1 [R=301,L]
If not working, rewrite log could be useful.

Related

changing url with get paramater in .htaccess not working

I am trying to change my url using htaccess but it doesn't seem to be working. I want http://example.com/blog_view?id=1 to be changed to http://example.com/blog-id/1/ but it isn't working. I have tried using this.
RewriteEngine On
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You might have to add a RewriteBase directive:
RewriteEngine On
RewriteBase /
RewriteRule ^blog-id/([^/]*)/$ /blog_view?id=$1 [L]
You can test your rules with this tool
You need one redirect and one rewrite rule (already existing):
RewriteEngine On
RewriteCond %{THE_REQUEST} /blog_view\?id=([^\s&]+) [NC]
RewriteRule ^ /blog-id/%1? [R=302,L,NE]
RewriteRule ^blog-id/([^/]+)/?$ blog_view?id=$1 [L,QSA,NC]

.htaccess rule redirecting, but not rewriting

I am writing a simple rewrite rule.
I want all trafic going to a given path to get contetns from another
I have these rules:
Options +FollowSymlinks
RewriteEngine on
#this is the rule I am implementing
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [L]
#This rule is working
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^([a-zA-Z]+)$ "/foo/bar/index.php?fb=$1&%1 [L]
There is one rule more after this one and it's working:
Like before it does not work. Like this, it does:
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [R,L]
I want to rewrite, not redirect, but I often use the R just to test.
I am probably doing something wrong, but what?
Note: I had another rule before, for rewrite, that worked. Its not missing any modules (I think).
After some fighting I discovered the problem.
For this rule to work:
#this is the rule I am implementing
RewriteCond %{REQUEST_URI} ^/bar/$
Rewriterule ^(.*)$ "/foo/bar/" [L]
It needs to be written like:
RewriteCond %{REQUEST_URI} ^/bar/index.php$
Rewriterule ^(.*)$ "/foo/bar/" [L]
It needed the index.php. You should replace for the file or resource that you have.

htaccess simple rewrite rule doesn't work

I've never did rewrite rules and everything I try out and find at google doesn't work. Especially creating multiple rewrite rules is hard for me because I don't know how the right syntax is and how a proper implementation looks like (1 Rewrite Condition or multiple and similar questions).
Thus I would be happy to get a result for my following try:
https://www.domain.com/our_accounts.php -> https://www.domain.com/accounts
The http -> https rule is already working. Maybe there is also the problem with my rewrite rules because I may need to add them before my https rules ?? I hope you guys can help me with this.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
# First rewrite to HTTPS:
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Now, rewrite any request to the wrong domain to use www.
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Rules for readable URLs
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
RewriteRule ^accounts$ /our_accounts.php [L]
</IfModule>
Try your htacess file this way. I also condensed the HTTP and www to one rewrite rule. Just replace yoursite.com with your real site.
<IfModule mod_rewrite.c>
RewriteEngine On
# First rewrite to HTTPS and redirect www:
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.yoursite.com/$1 [L,R=301]
RewriteRule ^our_accounts.php$ /accounts [R=301,L]
# Rules for readable URLs
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
</IfModule>

htaccess from https to http

I have these vales in my htacces file it all works fine except when i try to go back to http from https i have tried swapping the rules around with no success, any help would be awsome
I have tried all the sujestion with still no suucess so maybe i need to show you guys the entire thing.
Still not going back to http from https, here is the whole thing
Have i got the rules in the wrong order? im lost
<ifModule mod_rewrite.c>
RewriteEngine on
# For Sales:
RewriteRule ^shop/sales/?$ sales.php
# For the primary categories:
RewriteRule ^shop/([A-Z-Aa-z\+]+)/?$ shop.php?type=$1
# For specific products:
RewriteRule ^browse/([A-Za-z\+]+)/([A-Za-z\+\-]+)/([0-9]+)$ browse.php?type=$1&category=$2&id=$3
#For https pages:
#RewriteCond %{HTTPS} on
#RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L=301]
#RewriteCond %{HTTPS} off
#RewriteRule ^(checkout\.php|final\.php|admin/(.*))$ https://{HTTP_HOST}/$1[R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ /%1 [R=301,L]
</ifModule>
I am not able to test the rules, but I think you need to change the following:
In the first rule your flag is attached to the 2nd argument. This should create an internal error. Your second rule would rewrite all url's to their http equivalent, making an infinite loop. You need to make sure it doesn't match url's that you want to be in https. You can do this with %{REQUEST_URI} and a negation (!). As far as I am aware, L=301 is an invalid flag too. You probably meant to make it R=301.
RewriteCond %{HTTPS} off
RewriteRule ^(checkout\.php|final\.php|admin/(.*))$ https://{HTTP_HOST}/$1 [R,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !/(checkout\.php|final\.php|admin/(.*))$
RewriteRule ^http://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Last but not least an word of advice. Don't test your .htaccess with permanent redirects until everything works as expected. The browser will cache permanent redirects, not picking up further tries to make your .htaccess work as you want.

htaccess for redirect to SSL

For the past few hours (days) I have been having some trouble redirecting a
page to SSL.
My setup is as follows: I have the following .htaccess for an e-commerce site
on Apache 2.2.16 on Debian (all required mods enabled)
RewriteEngine On
RewriteBase /shop
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
all requests are passed to index.php which acts as my controller and includes
other .php files as necessary.
I now want to use HTTPS for the checkout process which is a php script
cleverly called checkout.php
I thought it would be as easy as changing my .htaccess to:
RewriteEngine On
RewriteBase /shop
RewriteCond %{SERVER_PORT} !^443$
RewriteCond %{SERVER_URI} checkout\.php
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
RewriteCond $1 !^(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ index.php?/$1 [L]
so that checkout.php is not processed by index.php.
Apparently it is not that simple. I could probably do it by using a hardcoded
https link to checkout but I would prefer to do it with mod_rewrite.
If anyone can share some insight into
this it would be really appreciated.
Thanks in advance
There are a few problems. First, the pattern in your first RewriteRule
RewriteRule ^checkout.php?/$1 https://localhost/shop/checkout.php?/$1 [L,R]
is written incorrectly. $1 isn't meaningful there (it's a capture result, but no capture has happened yet), and also the query string (part of the request after the ?) isn't part of what's matched, as the RewriteRule documentation says.
Second, I think you meant to use REQUEST_URI instead of SERVER_URI.
So I think you want something like this:
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/checkout\.php
RewriteRule .* https://localhost/shop/checkout.php [L,R]
RewriteCond %{REQUEST_URI} !^/(index\.php|products|img|theme\.php|checkout\.php)
RewriteRule ^(.*)$ /index.php?/$1 [L]
A few notes:
You don't need to match or add back in the query string in the first RewriteRule; mod_rewrite will automatically add it back in.
It's conventional to test RewriteCond %{HTTPS} off instead of
RewriteCond %{SERVER_PORT} !443, as #Jon Lin suggests.
You may want to add the QSA flag in your second RewriteRule.