Problem with url-rewriting for a multi-language website - php

I have tried to set somes url rewriting rules for my multi-langage website.
It was working, and I wanted to apply some corrections, and now it is not working anymore.
When I tried this url : http://mywebsite.fr/fr/ , the browser changes the url for http://mywebsite.fr/fr/?lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr&lang=fr
Here is the code:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(fr|en|nl)/$ index.php?lang=$1&%{QUERY_STRING} [L]
I said to myself that kind of bug couldn't come from url-writing, so in my php code I have put an "return false" as the very beginning of the page, the problem still occurs whit a white page...
Also, if I disable all the url-rewriting rules, I recevied an apache error "Not found"...
I have also tried to restart Apache, same problem...
Anybody have an idea ?
Thanks !

What you see is a typical rewrite loop. The cause is that you unconditionally rewrite, regardless whether the goal of rewriting has already been achieved, so whether rewriting has already been performed before.
You can get around that using a condition:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{QUERY_STRING} !^lang=
RewriteRule ^/?(fr|en|nl)/?$ /index.php?lang=$1 [L,QSA]

Related

mod_rewrite for URL with two URL-parameters

I want to be able to open the following url
http://example.com/login/12345
and in the background, it should load:
http://example.com/index.php?endpoint=login&access_key=12345
Obviously, I have tried many htaccess generators to create an appropriate RewriteRule set.
For example the following (from: http://www.generateit.net/mod-rewrite/index.php):
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
Even though I know for a fact that .htaccess and mod_rewrite is enabled on my server (i tried to setup a test to redirect all requests from example.com to example2.com using htaccess and mod_rewrite, and it worked), I can't get this to work.
I have now spent nearly 2 hours to find for a solution on stackoverflow and other websites, but all my attempts failed.
I hope someone can help me find out the correct way to rewrite my url in the above example.like explained above.
Thanks a lot in advance!
Try with this:
RewriteEngine On
RewriteRule ^([^\/]*)/([^\/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
#-Added-these---^--------^
When tested, here is the result:
Add this to the .htaccess file in your DOCUMENT_ROOT
Assuming login is not variable
RewriteEngine On
RewriteRule ^login/(\d+)$ index.php?endpoint=login&access_key=$1 [L,NC,DPI]
** If login is variable**
This is in case you want to redirect not only for login, but also for urls such as http://example.com/housenumber/12345
RewriteEngine On
RewriteRule ^([^/]+)/(\d+)$ index.php?endpoint=$1&access_key=$2 [L,NC,DPI]
Tested on actual Apache 2.2 and 2.4 :)

RewriteRule .htaccess multiple params

I'm trying to rewrite my url, but I'm getting an error "500 Internal Server Error"
I've never done this before, and I've read some tutorials but I can't say I got any smarter from it.
The .htaccess file is in the root directory (/var/www)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sub_domain/directory_to_my_files/
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*)/$ /index.php?pos=$1:$2&text=$3
The current link goes like this:
http://sub_domain.my_domain.com/directory_to_my_files/index.php?pos=(float|integer-only):(float|integer-only)&text=(any-text)
But I'm trying to do this:
http://sub_domain.my_domain.com/directory_to_my_files/(float|integer-only):(float|integer-only)/(any-text)/
Sorry if the links is a bit hard to read.
This should be placed in /directory_to_my_files/.htaccess
RewriteEngine On
RewriteBase /directory_to_my_files/
RewriteRule ^(-?[0-9.]+)[:-]([0-9.]+)/([^/]+)/?$ index.php?pos=$1:$2&text=$3 [L,QSA,NE]
I've tested this locally:
( I did not need the rewrite base )
RewriteEngine on
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*) /index.php?pos=$1:$2&text=$3 [R,L]
And it has redirected me
from
http://loc.domain/555-666/misc-text
to
http://loc.domain/index.php?pos=555:666&text=misc-text
I reckon this was what you wanted?
About the [R,L] at the end:
the R is telling apache to actually redirect, not just rewrite the url - this helps with testing.
the L is saying this is the last rule
Edit: could it be that the rewriteBase is causing you problems? Try it without and see if it works - at least you can nail where the problem is, then.

mod_rewrite not working to change URL

I want to change dynamic URL's to URL's more acceptable by search engines.
For example change this :
http://myurl.com.au/page.php?id=100&name=myname
to
http://myurl.com.au/100/myname.php
or .html at the end it does not matter.
I am using Apache 2.2. I am not using .htaccess rather I put my code in /etc/httpd/conf/vhosts/myfile
but it does not work, the URL does not change at all.
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /page.php?id=$1&name=$2 [L]
What am I doing wrong?
Either your have the wrong description in your question or your rule is backwards. Maybe this could work:
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(.*)&name=(.*)$
RewriteRule ^/page\.php /%1/%2.php [L]
After doing some further testing it turns out that I do have the right code. I just don't have my head screwed on and was not thinking. Expecting that the mod_rewrite would magically change the URL to symbolic links when it is actually doing the reverse of that. It is all working for me now.

need to change htaccess rewrite code

I have just had a problem with a Codeigniter site where, after the hosting company had migrated files to a new server, I could no longer navigate away from the home page. I Googled a forum with a similar issue and the answer was my htacess file. It was previously like this:
Options FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
and the given solution was to add a '?' character after 'index.php'. I did that and everything then worked OK.
I have tried hard to understand htaccess code and syntax and read many documents but they might as well be written in Chinese for all the sense I can get out of them. So can anyone explain why that additional '?' was needed and what it did? And if you can explain the rest of the code too I would be eternally grateful!
Your new host's php handler or webserver isn't setup to handle PATH INFO, or the stuff after the script (index.php) that's part of the actual path, e.g.
/index.php/this/is/part/of/the/path_info
The index.php script is executed, and everything after can be fetched via "PATH_INFO". If the server doesn't handle this, code igniter can optionally handle the path passed in as the QUERY STRING. Which are parameters that follow a ?.
None of this has anything to do with htaccess or mod_rewrite. It's just the way URLs and handlers work.

Strange behavior with mod_rewrite

I am trying to perform this type of rewrite
http://sitename/foo/var1/var2 -> http://sitename/foo/index.php?/var1/var2
This is my .htaccess file(placed in the directory foo):
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
In my php script I am displaying the values of $_SERVER['REDIRECT_QUERY_STRING'] and $_SERVER['REQUEST_URI']
If I request a URL like http://sitename/foo/bar the values as expected are bar and /foo/bar respectively.
This also works as expected for: http://sitename/foo/admin and http://localhost/foo/bar.
However when I try to access http://localhost/foo/admin (using localhost instead of sitename) the REQUEST_URI changes to /admin/?admin (this is how it displays in the address bar too, i.e http://localhost/foo/admin/?admin)
I searched for any .htaccess files that might be conflicting and also turned on mod_rewrite logging at level 6 but was unable to find any info.
I have no clue what might be causing it. It would be great if I could know what might be causing this, otherwise I might switch to nginx.
Thanks all for replies. This was some weird server configuration error, things seem to be fine on the new VM I installed. #Dan Grossman , thanks for your suggestion about using $_GET, some of my code is simpler than earlier.

Categories