how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS - apache

why this htaccess has error. it says "to many redirect" please help thanks
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]
I cannot try the wrong code, it always appear error

There are several issues with your .htaccess. I would suggest both re-ordering and editing the rules.
This would be a better approach, in my opinion. I'll explain the parts below.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]
# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Structure
First, general options and settings should come at the top and only once, i.e.
RewriteEngine on
RewriteBase /
Options +FollowSymLinks
Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.
Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.
This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess on the next RewriteRule match.
Omitting external redirect for index.php
I guess you added the index.php redirection handling after adding in the internal redirect, which probably has caused index.php to show up in the browser. So, with the above structure in place, I assume it will not show up again.
Error in last RewriteRule
As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule.
Avoid redirect loop
To avoid a redirect loop when rewriting to index.php with the last rule, you have to explicitly exclude index.php from language code redirection, i.e.
RewriteCond %{REQUEST_URI} !^/index.php [NC]

Related

How to htaccess url rewrite multi language

Can someone help me with the htaccess rewrite url?
I need to convert ?lang=en to /en
To always display the language used in the url
When someone comes to example.com to automatically 301 redirect them
to example.com/en as the main language.
When a user chooses another language to stay on the same page e.g.
example.com/en/contact.php
I have a total of three languages on the site en, de, hr.
The problem with my code is when I go to example.com/contact and select a language, it takes me back to the homepage example.com/en. If I type manually example.com/en/contact it works.
If I am at example.com/en/contact and choose another language e.g. /de url changes to example.com/en/de and a 404 error.
My current htaccess code looks like this
# Convert ?lang=en in /en
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
RewriteRule ^([bs|en|de]{2})/(.*)$ $2?lang=$1&%{QUERY_STRING} [L,QSA]
# Remove trailing slash from URL
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://example.com/%1 [R=301,L]
# Remove php extension in URL
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
With your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-SSL} !ON
RewriteCond %{HTTP_HOST} ^(?:www\.)reg\.bmi\.id$ [NC]
RewriteRule ^/?$ https://reg.bmi.id [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\scontact/contact\.php [NC]
RewriteRule ^ https://reg.bmi.id [R=301]
RewriteRule ^ contact/contact.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

redirect loop with mod_rewrite

I'm trying to add a language prefix to my pages through mod_rewrite
eg:
https://example.com/ > https://example.com/en/
and internally it need to translate to `https://example.com/?language=en
I've managed to do it for the base url, but when I try to do it with some other redirect implied, it's always end in an infinite loop.
eg:
https://example.com/product-p-22.html > https://example.com/en/product-p-22.html
and internally became `https://example.com/product_info.php?product_id=22&language=en
here is my actual config:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(en|fr)/
RewriteCond %{REQUEST_URI} .(php|html) [OR]
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} !language=
RewriteRule ^(.*)$ en/$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/example
RewriteRule ^(.*)/$ index.php?language=$1&%{QUERY_STRING}
RewriteRule ^(.*)/(.*)-p-(.*).html$ product_info.php?products_id=$3&language=$1&%{QUERY_STRING}
I've tried many flags like L, END, DPI or some combination of those, but with no luck.
when I look at the debug logs, it seems to find the right url, but then restart parsing the default url:
pass through /var/www/example/product_info.php
init rewrite engine with requested uri /product-p-22.html
pass through /product-p-22.html
any though on what I'm doing wrong here?
Server version: Apache/2.4.41 (Ubuntu)
Based on your shown samples, could you please try following. Please make sure you clear your browser cache before testing your URLs.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^en [NC]
RewriteRule ^(.*)$ en/$1 [NC,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php?language=en [NC,QSA,L]
RewriteRule ^([^/]*)/([^-]*)-p-([^.]*)\.html$ product_info.php?products_id=$3&language=$1& [NC,L,QSA]
Have it this way in your site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} !\s/+(en|fr)/ [NC]
RewriteRule ^ /en%{REQUEST_URI} [NC,R=301,L]
RewriteRule ^([a-z]{2})/[^-/]+-p-([^./]+)\.html?$ product_info.php?language=$1&products_id=$2 [L,QSA]
RewriteRule ^([a-z]{2})/$ index.php?language=$1 [QSA,L]

this mod_rewrite rule has me for a loop

I want to have /path/short serve up the content held by the file /path/short.html, but if the user types /path/short.html or /path/short/ I want him to be redirected (with 301) to /path/short. This is what I have so far in my .htaccess:
RewriteEngine On
RewriteBase /path
RewriteCond %{REQUEST_URI} !=/path/handle.html
RewriteCond %{REQUEST_URI} !^/path/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteRule ^path$ /path/short.html
#-------
RewriteRule ^short/$ /path/short [R=301,L]
# RewriteRule ^short.html$ /path/short [R=301,L]
The rules reflect the fact that handle.cgi will receive all other requests (ie, /path/this, /path/, /path/somethingelse, etc.) which is working great. So far, the /path/short/ redirects correctly to /path/short and /path/short is properly showing the content in /path/short.html, however, uncommenting the last line causes a loop. So, how do I get this to work?
You can use this code in /path/.htaccess:
RewriteEngine On
RewriteBase /path/
RewriteCond %{THE_REQUEST} /(short)(/|\.html) [NC]
RewriteRule ^ %1? [R=301,L]
RewriteRule ^(short)/?$ $1.html [L,NC]
A friend pointed me towards another question which helped me arrive at this solution:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ads/short(.html|/)?$
RewriteRule ^(.+) /cgi-bin/handle\.cgi?$1 [PT,QSA]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^short(.html|/)$ /ads/short [R=301]
RewriteRule ^short$ /ads/short.html
which works perfectly.

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)

mod_rewrite add REQUEST_URI to URL-parameter, handle it with ColdFusion

I have a strange problem with mod_rewrite in combination with ColdFusion:
I'm using the following rewrite rules in my virtualhost-configuration:
RewriteEngine On
RewriteRule ^$ index.cfm [L]
# Add trailing slash if missing
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(?i)^[\\/]CFFileServlet
RewriteCond %{REQUEST_URI} !index.cfm
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=301]
# Clean up multiple slashes in URL
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{THE_REQUEST} ^[A-Z]+\s//+(.*)\sHTTP/[0-9.]+$ [OR]
RewriteCond %{THE_REQUEST} ^[A-Z]+\s(.*/)/+\sHTTP/[0-9.]+$
RewriteRule .* http://%{HTTP_HOST}/%1 [R=301,L]
# Pass all non physically existing files/folders to index.cfm in URL.original_url
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/cms/
RewriteRule ^(.*)$ /index.cfm?original_url=$1 [QSA,PT,L]
Everything is working fine except for one thing:
If I call http://www.example.com/test/?asdf my URL.original_url reads like this original_url=/test/index.php&asdf and I can't figure out why there is index.php in it.
I tried already leaving out the PT-flag but it doesn't work without it. All pages e.g. http://www.example.com/test/ return 403 - Forbidden and in title of the page says "JRun Servlet Error".
Thanks for any ideas on this one!
EDIT:
Ok, I missed something important: the folder /test/ actually exists in my documentroot! Other URLs like /test/does_not_exist/ are not affected by the described problem.
Summed up:
http://www.example.com/test/ -> URL.original_url gets /test/index.php
http://www.example.com/test/does_not_exist/ -> URL.original_url gets /test/does_not_exist/ which is my expected behaviour.