Rewriting via htaccess and pass data - php

I have a multilingual website.
So I need to do something like this:
for example my site is: example.com and it has 2 foreign languages (en & de). Now i wanna install my site on example.com/foreign and if someone opens example.com/en/*anything* i want my website to open example.com/foreign/*anything* WITHOUT ANY REDIRECTION and also it should pass a parameter like LANG via POST or GET or anything else to detect the tables of database which are related to that language.
And the main problem is that *anything* can be anything like a directory name or something like test.php?par1=value1&par2=value2
so what's the solution for this?

Put this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z]{2})/(.+)$ /foreign/$1?lang=$1 [L,NC,QSA]

Related

Correct redirect (header redirect) and change the language on the site

I have some issues while developing my site. I want to realize multilanguage web site, but i don't know how to make correct redirecting for visitors.
For example, the user has come to my web site http://web-site.com/ua/ he sees the default language, and when he walk around through the site he needed to change to english language http://web-site.com/en/.
And now is the problem, how should i realize the next, when he is on product page or other, for example now is:
http://web-site.com/ua/automotive/wheels/product_url
and he is changing language, so now url would be look like this:
http://web-site.com/en/automotive/wheels/product_url
But i don't know how to make the redirect for the same url but with /en/ instead of /ua/. Can some one help me? Or what solutions would be better.
My .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^favicon\.ico
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
Or maybe it would be better make some redirects in .htaccess from http://web-site.com/ to http://web-site.com/en/. But this is another question.
I did my own router, not like the usual MVC routers, but very similar. If necessary, i will add it below at this question.

CMS Pages in Database .htaccess configuration

In my CMS I store all pages in a database. If you request a page currently it works like this:
Requested URI: www.xyz.com/site.html
.htaccess: mod_rewrite creates: /index.php?q=site
index.php: Looks up in Database for the entry site.
To clean up the URLs I like to have URLS like this: www.xyt.com/about/site.html or www.xyt.com/about/groups/groups.html
In my Database is an entry for every Page called owner which represents the Parent Site.
The Problem for me is that the number of 'folders' is not fixed.
So i thought I should Change www.xyt.com/about/site.php to /index.php?q=about-site in the .htaccess and than write a PHP function which finds the site site with the Parent about
What would be the RewriteRule?
I that a good way or is there an other (better) way?
Changing the foo/bar/about/site/etc.html to index.php?q=foo-bar-about-site-etc is much more difficult with mod_rewrite than it is with PHP. Just do this in php, get the $_GET['q'] variable and explode the string into an array or something using the / flags. It's also better this way because you'll know for sure that the / characters are reserved and you won't end up having to resolve stuff like /foo-bar/about/site. The rules would look something like:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+index\.php\?q=([^&\ ]+)
RewriteRule ^ /%1.html [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)\.html$ /index.php?q=$1 [L]

RewriteRule different pages per user

I am deciding to create separate profile links for each user who registers on the website. I am using the .htaccess file RewriteRule to achieve this. The url I want to achieve should look something like www.domain.com/username. (I don't want www.domain.com/users/username)
Now the problem is, if I add a rule like RewriteRule ^(.*)$ /users.php?username=$1
it will matchup all URL addresses for www.domain.com, which may direct to any path. Even if I would like to visit www.domain.com/about page, it will be redirected to
www.domain.com/users.php?username=about, which I don't want. (Even if the requests was www.domain.com/products/abc)
I could apply filters in my users.php to filter such usernames, or if a username is not found in database, redirect to the page, but this means I have to change filters every time I decide to add a new page in my directory (in this case, any). Also, all traffic will be directed through 1 page, which may cause a performance problem.
Please suggest a way to achieve this, as There are websites that work like this. A possible RewriteRule or PHP code to achieve this.
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)/?$ /users.php?username=$1 [L,QSA]
I always use just simple rewrite as below:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)(.*)/?$ index.php
All traffic is redirected to index.php and using php I can run specific controllers depending on url. You should also think about. Almost all frameworks use such rule.
Then in your PHP file you should examine
$_SERVER['REQUEST_URI']
variable to route request to specific controllers

URL Routing method for core php

I have web site in core php and I want to make my SEF URL.
In my web site for transferring control from one page to other i have used something like
header("Location: edit.php?id=12");
I read one article but couldn't get it how to implement it.
Link of article
So how i can make url to read like "mysite.com/usr/edit"
I read lot on google i got the idea that i need to do something in .htaccess but i dont know what needs to do with url in php so i dont have to change major code in site.
Thanks in advance.
You can do it with the .htaccess code given below:
RewriteEngine On
DirectoryIndex index.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
after creating .htaccess file with above code. You will have all the stuff after the file name ex. "/usr/edit" in the GET parameter $GET['url']. After that you can do anything.
Here is the working example of my MVC framework: https://bitbucket.org/ManthanB/my-framework/
You can contact me for further information.

Site URLs using a PHP "pretty url" framework

I have been experimenting with the lightweight NiceDog PHP routing framework, which routes like this:
R('entries/(?<id>\d+)')
->controller('Entries_Controller')
->action('show')
->on('GET')
Now the .htaccess file is set up to do this redirect like so:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
My problem is when I want to make a URL to somewhere, it seems impossible to do so.
Say I've requested the page entries/5, and on that page I would like to link to another entry entries/6:
Next Entry
This resolves to the address http://localhost/folder/to/project/entries/5/entries/6
Not what I want.
The href /entries/6 would link to http://localhost/entries/6
Also not what I want.
To work around this, I created a function to handle this problem:
function url($route) {
return "http://localhost/folder/to/project/$route";
}
So I can now write
Next Entry
which now links to http://localhost/folder/to/project/entries/6, which is exactly what I want.
However, I have to do this for EVERY in-site link, and it seems like there could be a better solution that doesn't involve an externally created URL.
Is there a "better" way to fix this problem? Is this a "common" problem with PHP frameworks? (It seems it would be)
The easy alternative would be to use <base href="http://example.org/your/project/index" /> in your page templates <head>. But that's basically like having full URLs generated. And yes, it's also valid for XHTML and still in HTML5.
I don't know about NiceDog but other frameworks I have used have a built in function that can convert a route to the corresponding URL.
For example in Symfony this would look something like:
Link Text
The routing system will then reverse resolve this into the URL relative to any root you set in the config.
Could you use:
RewriteBase /folder/to/project/
in your htaccess file making
RewriteEngine On
RewriteBase /folder/to/project/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html

Categories