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.
Related
To remove trailing extension (a .php in this case), I have tried the following two set of rules
RewriteCond %{THE_REQUEST} (\S*?)/(.*?)\.php [NC]
RewriteRule ^ %1/%2 [L,R=301]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule !\.php$ %1.php [NC,L]
and
RewriteRule ^(.*?)\.php$ $1 [NC,L,R=301]
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteRule !\.php$ %1.php [NC,L]
To convert a url like test.com/test.php to test.com/test, the first one works while the second one ends up looping infinitely. The rewrite_log is just confusing, so can anyone tell what is the problem with the second set?
PS: The rules might be inefficient or even not sufficient for a case like test.com but that's not the issue. I just want to understand how the rewrite is working BTS.
As mentioned in the comments, in the first case, there is no looping using %{THE_REQUEST} as the value is static and doesn't change once it is received by the server even after redirecting / "looping". So the rule will remove trailing .php and redirect. As the rewrite rule is substituting values from %{THE_REQUEST} in the rewritecond, after redirection, the value will not contain the trailing .php and thus proceed on to the second rewritecond and then finally redirect internally. After a second redirect, both the conditions will fail to match thus stopping the process
Whereas in the second case, the rewrite rule checks values from the uri which will remove .php and redirect. After redirection, the second rule will internally redirect back to the uri with php extension. Then finally in the supposed third final check which is supposed to not match both the rules, the first rule will again be matching thus infinitely looping the entire process.
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]
I can't understand and can't find the needed clear information about it.
Is it even possible to rewrite with mod_rewrite localhost/mysite/index.php?a=search url to localhost/mysite/search?
I have tried with wikipedia http://en.wikipedia.org/w/index.php?title=Dog, then it immediately redirects to https://en.wikipedia.org/wiki/Dog. I want similar thing.
I've tried this code on my localhost site .htaccess file:
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L]
it worked when I tried to type in browser something like localhost/mysite/search but it is only one part of what I want. Please answer, I'm absolutely exhausted.
You need an additional redirect rule for redirecting to pretty URL. Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?a=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L,QSA]
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.
I have the following Rewrite Rules set up:
RewriteEngine On
RewriteRule ^api/([A-Za-z0-9-]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([A-Za-z0-9-]+)$ index.php/other/$1 [NC,L]
Unfortunately these cause my server to throw a 500 error. Taken individually, they both work fine though.
My intention is that if the request is http://somesite.com/api/whatever/, the first rule will get triggered, redirecting to index.php/api/whatever/
If anything other than "api" gets sent as the second segment though, it will redirect to index.php/other/whatever.
Is my understanding flawed somehow? I thought that it would go down the list, and with the L flag, would stop executing once it hit something. Or is my syntax wrong?
Cheers
whenever you get a 500, check /var/log/httpd/error_log (or the equivalent path on your system.)
I'm pretty sure the hyphen char in your character group is a regex syntax error. (also, the [NC] flag makes [A-Za-z] redundant
Try:
RewriteRule ^api/([-A-Z0-9]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([-A-Z0-9]+)$ index.php/other/$1 [NC,L]
Or perhaps
RewriteRule ^api/([^/]+)$ index.php/api/$1 [NC,L]
RewriteRule ^([^/]+)$ index.php/other/$1 [NC,L]
I think you need QSA flag, try like that :
RewriteRule ^api/(.*)$ index.php/api/$1 [QSA,NC,L]
RewriteRule ^(.*)$ index.php/other/$1 [QSA,NC,L]