htaccess rewrite causing infinite redirect - php

I need to rewrite a URL from something like
/index.php?option=com_scoreboard&view=scoreboard&agent=001C0000016rJeUIAU
to
/quote/?agent=001C0000016rJeUIAU.
Here's what I've got so far.
RewriteCond %{QUERY_STRING} agent=(\w+)&?
RewriteRule ^index.php /quote/?agent=%1 [R=301,L]
Which works fine and dandy, but it ends in an infinite loop. And I know why too, it's because it keeps finding agent=. What should I be adding to my rewrite rules to stop this?
I've also tried variations like
RewriteCond %{QUERY_STRING} ^option=\w+?&agent=(\w+)&?
RewriteRule ^index.php /quote/?agent=%1 [R=301,L]
But it ends in the same infinite redirect.
This is for a Joomla site as well, if that helps. So after this rule is the standard Joomla rewrites.
Thanks a bunch!

Bette to THE_REQUEST variable instead and make sure to keep this rule as the first rule:
RewriteCond %{THE_REQUEST} /index\.php\?agent=(\w+)
RewriteRule ^ /quote/?agent=%1 [R=301,L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

Related

How can i redirect /?lang=en to /en?

I'm having trouble redirecting a multilingual website one page. My redirection to htaccess doesn't operate properly. I need assistance with how this can be done. In my htaccess, when I add these two lines
RewriteCond %{QUERY_STRING} ^lang=en$
RewriteRule ^ /en? [R=301,L]
redirect working, but mysite.com/en show 404 not found. Someone help me with this?
Your redirect rule as shown in question is working fine. Bot you also need a rewrite rule to handle /en:
RewriteEngine On
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{QUERY_STRING} ^lang=([a-z]{2})$
RewriteRule ^ /%1? [R=301,L]
RewriteRule ^([a-z]{2})/?$ ?lang=$1 [QSA,L]
If your browsers ends up at mysite.com/en, your redirection does work, so your question is not how to redirect, but why there's no content at /en on your site. You need to investigate that.

.htaccess keep multiple GET params

Part 1 takes care of making example.com/fr behave like example.com?lang=fr, or example.com/fr/some-page.php like example.com/some.page.php?lang=fr etc.
Part 2, which I'm currently working on not working well yet, is to obtain a new GET param for other pages called page, in this case if there's login in the url.
Problem: It seems like part of the page loads twice when going to for example example.com/login or example.com/fr/login.
Maybe un-necessary details here but for instance it says Facebook Pixel Error: Duplicate Pixel ID:, and similar errors for other tags I use like Mixpanel, and then my JS just stops working. That's all I can say about the problems I see on my side. Best chance seems to be about looking for flagrant errors in the htaccess rules.
What should be fixed in the rules so the end goal of having the GET param page and lang work fine?
RewriteEngine On
# Part 1
RewriteCond %{THE_REQUEST} \s/+[^?]*\?lang=([^\s&]+)\s [NC]
RewriteRule ^ /%2/%1? [R=301,L,NE]
RewriteCond %{REQUEST_URI} !^/js/
RewriteRule ^([a-z]{2})(?:/([^/]+))?$ $2?lang=$1 [NC,L,QSA]
# Part 2 (this is the part I am adding, which isn't fully working well yet)
# anything looking flagrantly wrong? If for example we are on `example.com/fr/login`,
# according to rules in this htaccess file we should have 2 GET params, `lang` and `page`.
RewriteCond %{REQUEST_URI} login
RewriteRule .* index.php?page=login
# adding more pages the same way
RewriteCond %{REQUEST_URI} signup
RewriteRule .* index.php?page=signup
You can use the following rule as your Part1
RewriteEngine on
RewriteRule ^(en|fr)/login/?$ /index.php?page=login [L]
RewriteCond %{THE_REQUEST} \?lang=([^&]+)\sHTTP
RewriteRule ^ /%1%{REQUEST_URI}? [L,R]
RewriteRule ^(en|fr)/?(.*\.php)?$ /$2?lang=$1 [L]

Create pretty url's with GET variables

At this moment I have a situation in which pretty url's are rewritten so that everything goes by the index.php, while maintaining the $_GET variables. The htaccess looks as follows:
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3&type=$4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2
RewriteRule ^([^/\.]+)/?$ index.php?p=$1
This works fine. However, I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123.
Is this possible, and if so, how? All I've managed so far is creating a redirect loop.
For those interested, the redirect loop I created looks as follows:
RewriteCond %{QUERY_STRING} ^p=([^&]+)
RewriteRule ^/?index\.php$ http://www.example.com/%1? [L,R=301]
I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123
Yes it indeed possible but you will need to use THE_REQUEST variable for that.
RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+)&projectid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
You can build similar rule for other URLs as well.

/file.php to /file/ try out

I wanted to redirect/rewrite my name.php files to /name/
I found the solution on another topic (http://stackoverflow.com/questions/5527789/htaccess-rewrite-within-directory-hide-php-extension-and-force-trailing-slash)
Though, I wanted to learn it myself and started from scratch.
I first used this one, which makes eg .com/test/ show the content of .com/test.php:
RewriteEngine On
RewriteRule ^(.*)/$ $1.php
Then I tried the following, by itself, which redirects .com/test.php to .com/test/:
RewriteEngine On
RewriteRule ^(.*)\.php$ http://www.mydomain.info/$1/ [R=301]
So, both work on their own. But when I combine them, I get an loop error, even when I add [L] to it, which should mean the rules should only be used once. So this doesn't work:
RewriteEngine On
RewriteRule ^(.*)/$ $1.php [L]
RewriteRule ^(.*)\.php$ http://www.mydomain.info/$1/ [L,R=301]
I've probably made some stupid error but it seems logically to me...
Hope someone can point out my error.
Thanks.
Remove L-flag from first rule. That would stop "executing" and the second rule wouldn't be used. At the second rule you should keep the L flag, because it is the last one.
Because you have an external redirect with the R=301, adding L to it doesn't help as much as you need, as the redirect will come back to the server as a brand new request - where it again matches your first rewrite rule.
Instead, you need something like this:
RewriteCond %{THE_REQUEST} ^\w+\ /(.*)\.php\ HTTP/
RewriteRule ^ /%1/ [R=301]
RewriteRule ^(.*)/$ $1.php
Note that THE_REQUEST matches the entire line of the original request, e.g. GET /index.php HTTP/1.1. Even when %{REQUEST_URI} is rewritten to .php as part of the 2nd rule (where it will match on an internal sub-request), %{THE_REQUEST} is never rewritten, and this will ensure that the URL rewritten to .php doesn't match on the sub-request and result in another redirect sent back to the client.

mod_rewrite for multiple requests not working

Perhaps someone can help me with this before I go completely crazy with it. I have a site with two urls which I want to redirect.
I want all traffic except the gallery pages to go to index.php.
This is my htaccess:
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery(/((([a-zA-Z0-9-]+)(/(\d+))?)/?)?)?$ gallery.php?groupId=$4&showpage=$6 [NC,QSA,L]
This works except for one part. The rewrite rule for the gallery is not fully working. It is sending through the groupId to the gallery.php script but I am not getting through the showpage argument. In fact when the showpage is included in the url I get a 404. So for instance.
These work and are handled correctly by the rewrite rule
gallery
gallery/
gallery/mygroup
gallery/mygroup/
This does not work and throws 404's.
gallery/mygroup/2
Nothing I do seems to fix this and I would appreciate your help on this. If I set the showpage. The gallery script works if I feed it the old
gallery.php?groupId=mygroup&showpage=2
so I am sure the htaccess rule is not catching the url.
Thanks in advance.
Your regex seems a little over-the-top. Use two simpler rewrite rules instead:
RewriteRule ^gallery/([a-zA-Z0-9-]+)/?$ gallery.php?groupId=$1 [NC,QSA,L]
RewriteRule ^gallery/([a-zA-Z0-9-]+)/(\d+)/?$ gallery.php?groupId=$1&showpage=$2 [NC,QSA,L]

Categories