Rewrite rule. .htaccess . ERR_TOO_MANY_REDIRECTS - .htaccess

I added the following Rewrite rule to .htaccess:
RewriteEngine on
RewriteRule ^page/?$ index.php?cat=123 [L]
and it loads content of /index.php?cat=123 when I go to www.site.com/page
The problem is that I also need to set up redirection from www.site.com/index.php?cat=123 to www.site.com/page
but when I try to do this I get "too many redirections" error. Any idead how fix this?
RewriteEngine on
RewriteRule ^page/?$ index.php?cat=123 [L]
RewriteCond %{QUERY_STRING} ^cat=123$
RewriteRule ^index\.php$ http://www.mysite.com/page [L,R=301]

Related

Simple Rewrite Rule issue

I have a bit of hard time trying to understand what is wrong with the 3rd line of this rewrite rule :
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*)\.html$ $1.php [L]
RewriteRule ^fr/lecon-de-kite\.html$ kite-lessons.php?lang=fr [L]
RewriteRule ^fr$ index.php?lang=fr [L]
RewriteRule ^it$ index.php?lang=it [L]
when I try to reach http://localhost/kitemeup/fr/lecon-de-kite.html I get an Not Found error
Have it like this in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^fr/lecon-de-kite\.html$ kite-lessons.php?lang=fr [L]
RewriteRule ^(it|fr)/?$ index.php?lang=$1 [L,QSA]
RewriteRule ^(.+)\.html$ $1.php [L,NC]
Just understand that ordering of rules is important as mod_rewrite processes them from top to bottom. Your html -> php rule was taking precedence and converting all .html URIs to .php making your 2nd rule ineffective.

Direct subdirectory to subdomain with htaccess

I want to redirect this: https://example.com/sub-dir/page
to this: https://subdomain.example.com/page
We use prestashop for our main site. We have some static content in subdictory that we've since moved to a subdomain. Navigating form example.com/sub-dir will direct to sub.example.com with no changes to the htaccess.
The problem is the pages won't redirect. We get a 404 errors. with example.com/sub-dir/page
I've tried this but it doesn't work.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/sub-dir
RewriteRule ^(.*)$ https://subdomain.example/$1 [R=301,L]
I've also tried this:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule ^sub-dir/(.*)$ https://subdomain.example/$1 [L,R=301]
Also this:
RedirectMatch 301 ^/sub-dir/$ https://subdomain.example/
Try this on top of your .htaccess file:
RewriteEngine on
RewriteCond %{HTTP:Authorization} ^(.*)
RewriteRule . - [E=HTTP_AUTHORIZATION:%1]
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^sub\-dir$ sub/ [L]
RewriteRule ^sub\-dir/(.*)$ https://sub-domain.example/$1 [R=301,L]

https and www not working properly

As I tried so many things to redirect www to non-www of subdomain via htaccess but not going well
I want to like this
https://test.example.com
but while I write like this
https://www.test.example.com
then the webpage gives me error like this
"Your connection is not private"
i tried this code in htaccess but not working properly
RewriteCond %{HTTP_HOST} ^www\.([^.]+\.example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
this code only redirects if i didnot mention https in www.test.example.com
To redirect everything to htpps I use the following. It is at the top of the .htaccess.
I have added your two lines for redirecting from www to the domain after the forwarding to https, and changed the HOST_HTTP to HOST_HTTPS.
Please note I haven't tried it.
Perhaps you can remove this if it doesn't apply: RewriteRule ^index\.php$ - [L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://test.example.com/$1 [R,L]
RewriteCond %{HTTPS_HOST} ^www\.([^.]+\.example\.com)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
</IfModule>

From http to https - htaccess and URL rewrite - Too many redirect error

I have a site with the following working code in .htaccess to get friendly URLs:
RewriteEngine On
RewriteRule ^([^_]*)_([^_]*)\.html$ /?s=scheda&id=$1&slug=$2 [L]
RewriteRule ^([^/]*)\.html$ /?s=$1 [L]
Since I installed a SSL certificate, I need to update my .htaccess to redirect http contents to https. I found this code:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
And I tried to add it to my .htaccess:
RewriteEngine On
RewriteRule ^([^_]*)_([^_]*)\.html$ /?s=scheda&id=$1&slug=$2 [L]
RewriteRule ^([^/]*)\.html$ /?s=$1 [L]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
but I get a "Too many redirect problem" error.
What's wrong?
Thanks a lot.
This error indicates you are looping, and I can see only one rule that could loop :
RewriteRule ^([^/]*)\.html$ /?s=$1 [L]
This redirects a.html to ?s=a.html which matches this same rule.
But it shouldn't work in http too. Can you confirm ?

301 redirection from index.php only on main page

I need to redirect /index.php to the main site without index but only on the main page.
I use this code in htaccess
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But the problem is that in Admin panel there is routing that needs index.php for pages to work properly. And this rule is deleting index.php everytime it appears on any page.
I tried adding line like this
RewriteCond %{REQUEST_URI} !^/AdminPanel/index\.php.*$
before RewriteRule but it doesn't change anything.
Hope you will help.
Remove .* from your regex:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/index\.php
RewriteRule ^index\.php$ / [R=301,L]

Resources