Default language for .htaccess - .htaccess

I believe what I'm trying to do is simple but I've been at it for hours and can't find the solution.
Basically I have a site that has 2 languages: English and French.
I would like that if the url has no language, it redirects to a page,
but if a language is specified in the url, redirect to that page.
For example
www.mysite.com -> goes to index.php?lg=en&page=$1
www.mysite.com/fr goes to index.php?lg=fr&page=$1
In my htaccess file I have this
RewriteEngine On
RewriteBase /
RewriteRule ^fr(.*)$ index.php?lg=fr&page=$1 [L,QSA]
RewriteRule ^(.*)$ index.php?lg=en&page=$1 [L,QSA]
This isn't working. Any help would be much appreciated!
Thanks
EDIT : The code I have supplied is kind of working... the only thing is that it doesn't load my CSS/JS anymore.. when I try to type the whole path of the CSS/JS file, it returns a page not found... I know the path is good because if I comment the first rewriteRule the files get loaded... I have no clue why this is happening.

Is RewriteEngine directive set to On in your Apache configuration? If not, the first line in your .htaccess should turn it on explicitly(RewriteEngine On should be the first line in the .htaccess).
Also you don't really need the second rewrite rule, do you? If no lg parameter is supplied via GET, you can default it to en in your index.php script.

Related

htaccess is working but does not replace the url

I'm trying to modify the subdomain name in the URL to make it look nicer. My current URL look something like:
www.mystore.com/productInfo.php?cPath=11_11&productID=222
So, I want to make it nicer by Rewrite in .htaccess in main with this code:
RewriteEngine On
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]
When I run and test it on the URL by typing www.mystore.com/productInfo/11_11/222 in the URL it works well. However, when this page is redirected by a different page or is 'refreshed' with a self redirecting a href= link(in which the link is written in php by the previous programmer), the above old link is still visible in the URL instead of the new one.
I am still a beginner while I suspect that I might need to change something in the cPanel/Apache(I think) for this but currently, I am still do not have access to the cPanel control. Is there anything that I might have missed to write in the .htaccess file or I really do need the cPanel control or any other reasons?
Any help is appreciated. Sorry that I could not find similar questions on this.
You can use the following :
RewriteEngine On
RewriteBase /
#redirect /productInfo.php?cPath=foo&productID=bar to /productInfo/foo/bar
RewriteCond %{THE_REQUEST} /productInfo\.php\?cPath=([0-9_-]+)&productID=([0-9_-]) [NC]
RewriteRule ^ productInfo/%1/%2? [L,R=301]
#rewrite new URL to the old one
RewriteRule ^productInfo/([0-9_-]+)/([0-9_-]+) productInfo.php?cPath=$1&productID=$2 [NC,L]

.htaccess rewrite from domain/ to domain/nl/

I recently installed Prestashop which has URLs in the form of domain.nl/my-account . Now I added a second language which turns URLS into domain.nl/nl/my-account or domain.nl/en/my-account
As people have bookmarked different items and searchengines have indexed the site I would like to redirect visitors to the old URLS (without the language) to be redirected to the same URL with language (NL is preferred).
A redirect to the mainpage is all I could achieve up to this point. Does anyone have an idea how to set this up using .htaccess??
Any help is greatly appreciated.
Try adding this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/(nl|en)/
RewriteRule ^(.*)$ /nl/$1 [L,R=301]
So you may need to change the (nl|en) part, because that regex checks to see if the URL already starts with /nl/ or /en/. You can replace the "en" with whatever other language you don't want redirected.

htaccess: how to get cpanel username out of url and redirect

I have the following htaccess lines...
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
The first line works fine, its the second line that doesn't work at all...
The first line does this....
domain.com/about -> domain.com/index.php?p1=about
What I'm trying to do with the second line...
if the url is server1.domain.com/~username/about....
I need it to translate to server1.domain.com/~username/index.php?p1=about
Basically, detecting if there is a ~
I am trying to work out my code to allow for the development url of the hostname/~username
Right now it is showing the green apache 404 not found page when trying to visit the website using that code.
Please let me know if you need any more information
Switch the order of the rules and add a RewriteBase:
RewriteBase /
RewriteRule ^~([a-zA-Z0-9]+)/([a-zA-Z0-9]+)?$ ~$1/index.php?p1=$2 [L]
RewriteRule ^([a-zA-Z0-9]+)?$ index.php?p1=$1 [L]
You need to be sure that this .htaccess file is triggered both on requests to pages from domain.com and pages from server1.domain.com.
Also, if you have a .htaccess file in any subdirectories (you shouldn't, based on your problem description), you will have to modify those accordingly, but we would need more information.
You need to place this rule in your httpd.conf:
RewriteEngine On
RewriteRule ^/?(~[a-zA-Z0-9]+)/([a-zA-Z0-9]+)$ /$1/index.php?p1=$2 [L,QSA]
And make sure this line is uncommented in httpd.conf:
LoadModule userdir_module modules/mod_userdir.so
Then make sure index.php is present directly under ~username/

htaccess mod_rewrite RewriteRule

I'm having difficulties to get this to work. Maybe someone can help me.
If a user opens
domain.com/folder/images/1.png
the server should rewrite to
domain.com/folder/images/image.php?file=1.png
since some rewrites are required elswhere on the site, I thought I should put the .htaccess file for this one into
domain.com/folder/images/
containing
RewriteEngine on
RewriteRule ^([\.\-_0-9A-Za-z]+)$ image.php?file=$1 [L]
but this somehow sets
$1=image.php
instead of using the actual filename. If I use
RewriteEngine on
RewriteRule ^([\-_0-9A-Za-z]+)$ image.php?file=$1 [L]
the redirect only works for strings without a period in it
If i can get the behavior for multiple folders with one single .htacces file in the root of domain.com it would be even better. Oh and btw, the file extension can only be jpg or png. Can i include that into the regular expression?
Help would be greatly appreciated!
You can put this in your root htaccess (or better in you vhost conf file and disable AllowOverride, to disable the use of htaccess)
RewriteRule ^folder/images/([\.\-_0-9a-z]+)$ folder/images/image.php?file=$1 [L,N]
N for case insensitive url.

PHP Rewrite Rules

The actually URL which my app uses is:
http://site.com/search.php?search=iPhone
but I would like it to be possible to achieve the same with
http://site.com/iPhone
I have no experience of rewrite rules, how can I set this up?
The solution has worked but the new URL is displayed in the address bar. I thought it would have been possible to set this up so that it appears as though the page location is
http://site.com/iPhone
without changing to display
http://site.com/search.php?search=iPhone
Is this possible? Thanks.
Create a file called .htaccess in the root of your website and put this in it.
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^(.*) search.php?search=$1 [R]
Should do the trick.
I would suggest however that you make it a bit more specific, so maybe require the user of a search directory in your url. eg instead of mysite.com/IPhone use mysite.com/search/IPhone which would work like
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteRule ^search/(.*) search.php?search=$1 [R]
This makes it easier to have normal pages that arnt redirected, such as about us or a basic homepage.
As Chris says, this is not PHP but Apache that does this, and whether it works can depend on your hosting setup.
You need to specify something like this in your .htaccess file:
RewriteEngine on
RewriteRule /(.*) /search.php?search=$1
Check also:
mod_rewrite: A Beginner's Guide to URL Rewriting
Module mod_rewrite, URL Rewriting Engine
Rewrite rules aren't part of PHP as far as I'm aware, but Apache (specifically mod_rewrite) or whatever server you're using. For Apache, you need on the server to have a file called .htaccess, and in it put something like:
RewriteEngine on
RewriteRule ^(\w+)/?$ /index.php?search=$1
^(\w+)/?$ is a regular expression - it matches any word of 1 or more characters, followed by a / maybe. So it changes site.com/iPhone into site.com/index.php?search=iPhone. Sound about right?

Resources