htaccess redirect - too many redirects - apache

I need to rewrite
www.domain.co.uk/store?cat=57
to
www.domain.co.uk/store/57
but I keep getting a 'too many redirects' error. I've got the following in my htaccess:
RewriteCond %{QUERY_STRING} ^cat=([^./]*)$
RewriteRule ^store\.php$ /store/%1? [R=301,L]
RewriteRule ^store/([^./]+)$ store.php?cat=$1 [L]
The redirect works if I comment out the bottom line, however this then stops my pages loading properly.
Any ideas?
Thanks,
Chris

You can use:
Options -MultiViews
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+store\?cat=([^\s&]+) [NC]
RewriteRule ^ /store/%1? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^store/([^./]+)/?$ store?cat=$1 [L,QSA,NC]

Related

apache http to https redirected you too many times

I have a website. I want to use https only on the page so i looked up for a code for my .htaccess and I found the following code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
After adding the code to my .htaccess I reload the page an get an error: redirected you too many times.
After removing the code from the .htaccess I reloaded the page again. It works fine. If I reload the page with https manually it works fine too.
So why I get the error?
Thanks for your help!
I ran with the same problem and I resolved my issue using a much more simpler code for the .htaccess:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

RewriteRule: match only first occurance of a string. Match /mystring but not /some/path/to/mystring

I have the following RewriteRule in .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^mystringtoreplace/?$ http://www.example.com/some/other/path/mystringtoreplace [L]
Intention is to redirect http://www.example.com/mystringtoreplace to http://www.example.com/some/other/path/mystringtoreplace.
What I get is a too many redirects error "ERR_TOO_MANY_REDIRECTS".
How can I write a correct RewriteRule for that problem?
In fact the RewriteRule is correct. There was a problem with WordPress Permalink. See comment above.

Problems with mod_rewrite and new url structure

I want to make new url structure on my site:
/page.php?id=1 -> /terms-of-service
/page.php?id=2 -> /faq
So i made in htaccess:
RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]
And it works good, when i go mysite.com/terms-of-service i can see my page number 1. But i also want to make a 301 redirect from old addresses to new ones. When i try to make it like this:
RewriteCond %{QUERY_STRING} ^id=1$
RewriteRule ^page.php$ terms-of-service? [L,R=301,NC]
RewriteRule ^terms-of-service /page.php?id=1 [L,NC,QSA]
I got an "Invalid redirect" error in my browser. How can I fix it?
Your rules will cause redirect loop. You need to use %{THE_REQUEST} here. Have it this way:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /page\.php\?id=1\s [NC]
RewriteRule ^ terms-of-service? [L,R=301]
RewriteRule ^terms-of-service/?$ page.php?id=1 [L,NC,QSA]

.htaccess - Redirect to a subfolder according to the preferred language

we have a website in English. Now we want to have a translated copy of this website in German.
The index.html is located in the root folder: www.mydomain.com/index.html
Now I want to create a htaccess file which redirects all user which have set the preferred language in their browser to German (de, de-de, de-at, de-ch) to www.mydomain.com/de/index.html. All other user should be redirected to www.mydomain.com/en/index.html.
I tried a lot but I can't make it work. I do not really understand the regular expression thing and it's usage.
Most of the tries ended up with error 403 or 500. The current htaccess doesn't end up in an error, but it doesn't work.
The htaccess is now:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^/(.*)$ /de/$1 [L]
RewriteRule ^/(.*)$ /en/$1 [L]
It seems like the file doesn't work at all. When I open www.mydomain.com it opens www.mydomain.com/index.html
Htaccess and rewriting works in general:
RewriteRule ^(.*)$ http://www.google.com/ [R=301,L]
works.
If I delete the slash behind ^
RewriteRule ^(.*)$ /en/$1 [L]
I get an error 500.
What do I do wrong?
I actually want the server to redirect to the webpage which the user tried to open. So if the user opens mydomain.com/index2.html he should be redirected to mydomain.com/xx/index2.html (and not to index.html)
Any ideas?
Thanks,
Jaroslaw
You can use this rule:
Options -Indexes +FollowSymLinks
RewriteEngine On
RewriteRule ^(en|de)/ - [L,NC]
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^(.*)$ /de/$1 [L]
RewriteRule ^(.*)$ /en/$1 [L]

Still having mod_rewrite problems / .htaccess

I have several rewrite rules in my .htaccess file that all work fine and I want to add a specific case to turn
sinaesthesia.co.uk/category/psoriasis
into
sinaesthesia.co.uk/category.php5?category=psoriasis
So I tried:
RewriteRule ^(.)category/(.)$ /$1category.php5?category=$2 [L]
which doesn't work. I've tried it without capturing the stuff before 'category' because category.php5 is at the root anyway, so it ought to work without that, have tried:
RewriteRule ^category/([a-z]+?)$ /category.php5?category=$1 [L]
with and without capturing the stuff before category, and nothing works - in fact, I generally get a 500 error! Here is the rest of the file:
RewriteEngine On
#remember to change this to aromaclear
RewriteCond %{HTTP_HOST} !^sinaesthesia\.co.uk$ [NC]
RewriteRule ^(.*)$ http://sinaesthesia.co.uk/$1 [R=301,L]
#Translate default page to root
RewriteCond %{THE_REQUEST} ^GET\ .*/index\.(php5|html)\ HTTP
RewriteRule ^(.*)index\.(php5|html)$ /$1 [R=301,L]
#translate any .html ending into .php5
RewriteRule ^(.*)\.html$ /$1\.php5
#change / for ?
RewriteRule ^(.*)\.html/(.*)$ /$1\.html?$2
#strip .html from search res page
RewriteRule ^(.*)search/(.*)$ /$1search_results\.html/search=$2
#translate product details link from search res page
RewriteRule ^products/(.*)/(.*)/(.*)$ /product_details.php5?category=$1&title=$2&id=$3 [L]
#Translate products/psorisis/chamomile-skin-cream-P[x] to productview.php5?id=1
RewriteRule ^products/.*-P([0-9]+) /productview.php5?id=$1 [L]
Ah: because I have a document called category.php5 and I'm trying to use category/psoriasis, the server tries to resolve that as category.php5/psoriasis, which fails. Fixed it now!