Rewrite Friendly php URL for multiple languages using .htaccess file - php

I have a couple websites that I built a CMS for that will save the languages in an XML file and depending on the page and language selector, it will show the correct data. I cuurently have 8 different languages for each of the sites and the urls are like:
http://leclosdamboise.com/index.php?lang=fr
http://leclosdamboise.com/rooms.php?lang=en
http://leclosdamboise.com/hotel.php?lang=de
I am trying to have it so that the urls are rewritten to look like this:
http://leclosdamboise.com/fr/index.php
http://leclosdamboise.com/en/rooms.php
http://leclosdamboise.com/de/hotel.php
I have spent hours combing through forums and not been able to find a solution that works. My .htaccess file currently looks like this:
SetEnv PHP_VER 5_TEST
SetEnv REGISTER_GLOBALS 0
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([A-Za-z0-9]+)$ index.php?lang=$1 [QSA,L]
This does not do anything and the urls are still the ugly step-daughter version. The links above are actual links to one of the sites. Once I sort this out I need to deply it to several sites, but been racking my head trying to sort this out. Can anyone tell me what I'm doing wrong?

This should work in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^([^/]+)/([^.]+)\.php /$2.php?lang=$1 [L,NC,QSA]

Related

Ignoring a subfolder with .htaccess

In my public_html I have 2 Folders, wordpress and tickets. I currently have the /wordpress directory working via mod_rewrite so that the URL's look nice. I'd like to setup an instance of OSTicket, and upon navigating to example.com/tickets to start the configuration, I'm hit with a Wordpress 404 error.
My root .htaccess looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/tickets/.*
RewriteRule ^tickets - [L,NC]
RewriteRule ^index\.php$ /wordpress/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /wordpress/index.php [L]
</IfModule>
I also have a .htaccess in the tickets folder, with;
RewriteEngine off
If anyone has some insight or perhaps something I should look into it would be most appreciated, thankyou all kindly.
The condition you have is the opposite of your match. They can't both be right. Drop the condition permanently, and amend the rule to this:
RewriteRule ^tickets(?:$|/) - [L]
That ensures /tickets-foo/ still goes to WordPress and that you're not matching case-insensitively when your filesystem is case-sensitive.
Temporarily rename the tickets dir. Create another one with only an index document in it, content is irrelevant but put something in. Visit example.com/tickets and you should see your aforementioned content.
I'm betting the issue is not in the .htaccess nor WordPress.

how to redirect to specific file by writing rule in .htaccess file?

I have developed a project without using any framework. I want to redirect a page to specific php file (example : movie/movie.php) when click on this
Click me
link. I have created .htaccess file and currently it look like this.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/movie/([0-9]+)/$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
I have enable mod_rewrite module in httpd.conf file. I am using wamp server in windows machine.
I dont no how to do this. I have referenced few sites and I created this above rule in my own. Kindly any one help me to do this and feel better if your answer have explonation as well. Thanks in advance.
Modify the rule as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^movie/([0-9]+)/(.*)$ http://localhost/tthtml/movie/movie_review.php [L]
</IfModule>
The rule that you placed was not working because it was comparing only the string "/movie/1/", while you actually want to match "/movie/1/Baashha-Suresh-Krishna-1995".
For this to work, I have modified the RewriteRule directive as ^/movie/([0-9]+)/(.*)$ to accommodate the remaining string "Baashha-Suresh-Krishna-1995" or any other value that the application may have.

.htaccess redirect root to one file, all other files to another

I have 2 domains using the same webspace, originaldomain.com and newdomain.com
I have placed a .htaccess file at the root of newdomain.com so that if visitors go to it, it shows (but masks) the url of:
originaldomain.com/script.php
I can get something similar using the following rules:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.originaldomain.com/script.php [P]
However, style sheets, images and javascript are not loading. I assume one of two things:
They are not applicable to the mod_rewrite rule and are not found
when originaldomain.com/item1.jpg is being searched for at
newdomain.com/item1.jpg
All files may be subject to having a php file stuffed before their
name, so that originadomain.com/item.jpg is now being looked for at
newdomain.com/portfolio.phpimage1.jpg
I'm looking to create a situation where:
If somebody visits newdomain.com/?variables it masks and loads
originaldomain.com/script.php?variables
If somebody visits www.newdomain.com/?variables it masks and loads
originaldomain.com/script.php?variables
All other file requests from the root of newdomain.com are pointed to
relative links at originaldomain.com, so that
http://newdomain.com/css/stylesheet.css is loaded and masked from
http://originaldomain.com/css/stylesheet.css
Any input appreciated, thank you.
From trial and error, this appears to be working at the moment:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^$ http://www.originaldomain.com/script.php [P]
RewriteCond %{REQUEST_FILENAME} !^$ [NC]
RewriteRule ^(.*) http://www.originaldomain.com/script/$1 [P]

Convert PHP Variable URL with mod_rewrite to clean URL

I need to convert a link that contains php variables into a clean link that can be picked up and indexed by Google. I've looked at ways to do this, and using mod_rewrite apparently is the best way to handle this.
Currently my URLs look like this:
http://mysite.com/jobs/view/?j=senior-model-validator-groep-risk-management
I'd like to end up looking like this
http://mysite.com/jobs/view/senior-model-validator-groep-risk-management
It's probably really easy but sadly I know nothing about .htaccess. I tried a few things but I end up with nothing remotely close :-)
Thanks in advance!
Edit: Here's my full .htaccess file based on the help so far:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
You may use an .htaccess file at the root of you web site with these rules:
RewriteEngine on
RewriteRule ^jobs/view/(.*)$ jobs/view/?j=$1 [L]
Do not forget to filter/validate GET input in the script dealing with the data...

Mod rewrite problem

At the root of my site... www.domain.com . want to add some static pages that the page url can be set from the user.
So if the users set as url profile then full page url should be www.domain.com/profile ..
So far a simple rewrite rule would do the job.
trasnlate it to something like /staticpage.php?tag=profile
The problem that i want some pages like www.domain.com/shop at the root which arent static...
So what can i do if all the requests for the main directory go to /staticpage.php?tag=$1 ?
I recommend using mod rewrite to send everything to your index.php file and using a front controller to do this. It makes it much easier.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
You'll find a lot more help about mod_rewrite on ServerFault as a general rule, but I tend to do this:
RewriteEngine on
RewriteRule ^static.*$ - [L]
RewriteRule ^assets.*$ - [L]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule .* /router.php
where "static" are uploaded files, and "assets" are production graphics/stylesheets/js libraries etc.

Categories