Create different URLs for different languages with htaccess - php

I am trying to create different URLs for different languages, for example http://localhost/shift/en without "en" being an actual folder (shift is an actual sub folder). I want the 'en' to be a variable I can use in my PHP code. Here is how my htaccess looks like:
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /shift/$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [QSA,L]
RewriteRule ^(en|de|fr)/(.*)$ $2?lang=$1 [L,QSA]
RewriteRule ^(.*)$ $1?lang=en [L,QSA]
Here's my PHP code:
var_dump($_GET['lang']);
When I go to http://localhost/shift/en it gives me a 404 and a PHP error: "undefined index: lang".

You need to edit your .htaccess like this-
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?lang=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?lang=$1
and the index.php:
echo $_GET['lang'];
If the url is- http://example.com/en, then the output will be- en
You can do the other things then, with the language.
If you want to give different versions of the page, I recommend you using a page for each language and include them if a language is asked for in the url.
For example- If someone types http://example.com/en, in the index.php you need to do this-
if($_GET['lang']=="en") {
$_SESSION['language']="en"; //Adding a session makes your life easier
include("en.php");
//everything else
}
I have updated the rewrite rule. Check it out.

Related

URL rewriting in .htaccess doesn't works as it should

I am currently coding my own website in PHP and MySQL on WampServer (local). I've already rewrited some of my URLs successfully. But I'm having problems with one of them.
I want to display on a page all my articles listed in a category using the GET method. For the moment, I have the following URL : http://localhost/actuco/cat.php?id=xpS3cc&slug=amerique-du-nord and I would like to use and display this URL as http://localhost/actuco/c-xpS3cc-amerique-du-nord/ (which does contains exactly the same GET parameters than the original URL).
I have tried to do this with the following line in my .htaccess file
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
When I write the second URL in my browser, it shows me a blank page with no code lines at all. My first URL works perfectly.
I'm really lost and I really don't know how to fix it.
Here is the whole .htaccess file used on my website (all other URL rewritings in this file do work).
Options +FollowSymlinks
RewriteEngine On
RewriteBase /actuco/
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_URI} /+[^\.]+$
#RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/([^/]*)/$ article.php?lng=$1&yr=$2&mo=$3&dy=$4&slug=$5&total_slug=$6
#RewriteRule ^([^/]*)-([^/]*)-([^/]*)$ waluty.php?cur=$1&amt=$2&lang=$3
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
RewriteRule ^c-([^/]*)-([^/]*)/$ cat.php?id=$1&slug=$2
RewriteRule ^bio$ o.php [L]
Thanks in advance for your help !
just pest this code in your .htaccess file
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L,QSA]
hopefully, it will work.
Solved ! Just forgotten to add slash before $ in this line
Before :
RewriteRule ^([^/]*)/([^/]*)$ url.php?mode=$1&u=$2
After :
RewriteRule ^([^/]*)/([^/]*)/$ url.php?mode=$1&u=$2
Now it works, but I still have a problem with multiple hyphens in slug described here : Multiple hyphen handling in .htaccess (URL rewriting)

mod_rewrite: Trailing slash on URL with two GET variables

I'm in the process of overhauling one of my projects, a web based booking system, but I'm having a bit of an issue with my htaccess file. The old system was pretty standard, with .php scripts in the route of the website, I had a rule hiding my extensions, and I resultantly had a URL like /viewinvoce?ID=1. I've been trying to find out how to rewrite this URL so it looks a lot neater - in the format /viewinvoice/1, and I'm getting there, but I have a slight problem...
The URL /test works - it adds a trailing slash making the URL /test/, and the value 'test' is passed to the webpage.
The URL /test/ works as above, a trailing slash isn't added since it already exists, and 'test' is passed to the webpage.
The URL /test/1 also works, 'test' and '1' are both passed to the web page,
but when a slash is type after 1 (/test/1/) the page throws a 404.
My .htaccess file
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?PID=$1&ID=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])/(.*[^/])$ http://%{HTTP_HOST}/$1$2/ [L,R=301]
My simple PHP script..
<?php
echo $_GET['PID'];
echo '<br>';
echo $_GET['ID'];
Ideally, I'd like the .htaccess file to add a second trailing slash to the second variable passed, but I'm a bit confused at this point, and ideally a second pair of eyes would be useful!
Try these rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?PID=$1&ID=$2 [L,QSA]
RewriteRule ^([^/]+)/?$ index.php?PID=$1 [L,QSA]
Make sure to test it after clearing your browser cache.

Redirecting existing URLs to SEO Friendly URLs

I have came accross a problem that every .htaccess query I've tried wasn't worked out ! I have URLs like this:
http://www.example.com/index.php?x=product
And I want to change it to a user friendly URL like this:
http://www.example.com/product/
Or it can be:
http://www.example.com/product.php
I've tried this code below:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^x=product$
RewriteRule ^index.php.*$ http://www.example.com/product.php? [R=302,L]
Now, it is redirecting perfectly, but this is not the problem. I've used this for only SEO so I must include http://www.example.com/index.php?x=product in the product.php file. Any help can be precious, thanks...
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /index\.php\?x=([^\s&]+) [NC]
RewriteRule ^ /%1/? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/.]+)/?$ index.php?x=$1 [L,QSA]
This will redirect /index.php?x=product to /product/ and will rewrite it internally to /index.php?x=product in 2nd rule.
You don't need to put anything in the product.php file. Make sure there is a .htaccess in the directory that has the files you want to make url/seo friendly.
To have it SEO friendly make sure its in this format
http://www.example.com/product/ not http://www.example.com/product.php
if you must have a file extension, have it in http://www.example.com/products.html (you want to tell the search engine the page isn't dynamic although it is to get better pagerank)
Insert this in your .htaccess
RewriteRule /(.*)/$ products.php?x=$1
the (.*) pulls the elements out and puts them in variables $1

Rewriting example.com/section.php?username=xyz to example.com/xyz/section

Using the following htaccess, I have been able to rewrite example.com/profile.php?username=xyz to example.com/xyz,
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Adding the following to the above,
RewriteRule ^([a-zA-Z0-9_-]+)/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/section/$ section.php?user=$1
did not resolve example.com/section.php?username=xyz to example.com/xyz/section.
Am I doing something wrong here?
First of all: The manner of speaking would rather be the opposite: The rules you showed are to rewrite requests like /xyz internally to /profile.php?username=xyz and not vice versa.
Now if you want to rewrite requests like /xyz/section internally to /section.php?username=xyz where section and xyz are variable, try these rules:
RewriteRule ^([a-zA-Z0-9_-]+)/?$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/([^/]+)/?$ $2.php?user=$1
To look for static files (images, css) in the right directory without having to write the file address, do:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Before writing the code suggested by RageD
(Sorry, should have posted it as a comment but I needed newlines)
I tested it and it works. Try this:
RewriteRule ^([a-zA-Z0-9_-]+)\/section$ section.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)\/section\/$ section.php?user=$1
All I did was escaping the / in the URL since / is a regex delimiter.

mod_rewrite for PHP's "lang?=" for more than one page?

I currently use the follwoing code in my .htaccess file
RewriteEngine On
RewriteBase /
# Redirect languages
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
With that code, every time I type for instance, /en at the end of the URL, it redirects me to /?lang=en (loads the the English content from a PHP array):
For instance:
example/en redirects me to example/?lang=en while keeping example/en in the URL.
But I also have a thanks.php page and the code above clearly just work for the index.php page.
How can I make the rewrite rule to work for both index.php and thanks.php page?
The most straight-forward way is just to do this:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteRule ^thanks/(en|es|zh\-tw|zh\-cn)/?$ thanks.php?lang=$1 [L]
If you want to make it more general, you have the option of white-listing files, like this:
RewriteCond $1 ^(thanks)/$ [OR]
RewriteCond index ^(index)$
RewriteRule ^(.+/)?(en|es|zh\-tw|zh\-cn)/?$ %1.php?lang=$2 [L]
...where (thanks) would be a pipe-delimited list of the files you wanted to have this functionality, or you can just accept every request as a pass-through to an existing PHP page:
RewriteRule ^(en|es|zh\-tw|zh\-cn)/?$ index.php?lang=$1 [L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?[^/]?)/(en|es|zh\-tw|zh\-cn)/?$ $1.php?lang=$2 [L]

Categories