htaccess rules - multi-language website - .htaccess

I created a language script in my website. This script recognizes the language in the URL and loads the language definitions.
For example:
RewriteRule ^(.+)/about/?$ sobre2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/?$ contato2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/products/?$ produtos2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=$1 <-- working fine
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=$1 <-- working fine
The pages above are accessed, respectively, as this:
https://domain.com/b2b/pt-br/about/
https://domain.com/b2b/pt-br/contact/
https://domain.com/b2b/pt-br/products/
https://domain.com/b2b/pt-br/contact/sales/
https://domain.com/b2b/pt-br/contact/general/
This is working fine in all pages, except index.
When I try to do the same thing with the index page, using this code:
RewriteRule ^(.+)/?$ index2.php?lang=$1
Besides it doesn't work, it also affects all the other pages (and resources), so the resources are not loaded and the page looks like a "pure html".
I'd like to know how can I configure the htaccess to make my "index2.php" be accessible like this:
https://domain.com/b2b/pt-br/
Thank you.

.+ will match 1 or more of any character and if you place rule before other rules then this will make other rules ineffective.
You can use these rules:
RewriteEngine On
RewriteBase /b2b/
RewriteRule ^((?!pt-br/).*)$ pt-br/$1 [L,R=301,NC]
RewriteRule ^(.+)/about/?$ sobre2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/?$ contato2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/products/?$ produtos2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/sales/?$ contato-vendas2.php?lang=$1 [L,QSA]
RewriteRule ^(.+)/contact/general/?$ contato-form2.php?lang=$1 [L,QSA]
RewriteRule ^([a-z]{2}-[a-z]{2})/?$ index2.php?lang=$1 [NC,L,QSA]

Related

how to replace "?" and "=" sign with "/" in URL - PHP htaccess

i am working on project, which is running XAMPP localhost and PHP MYSQLI,
my question : how i replace "?","=" signs with "/" slash. ?
like, my url is "archive?date=2017-06-02&p=4"
and i want to force it "archive/2017-08-02/4"
i found many codes on stackoverflow and some other sites, but that are not working for me.
if codes are working then, CSS files and GET method doesn't work on my project.
complete code of .htaccess is given below.
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^=]*)=([^=]*)=(.*) /$1/$2/$3 [N]
RewriteRule ^([^=]*)=([^=]*)$ $1/$2 [L]
RewriteRule ^home index.php [NC,L]
RewriteRule ^archive archive.php [NC,L]
RewriteRule ^about about.php [NC,L]
RewriteRule ^article article.php [NC,L]
RewriteRule ^news news.php [NC,L]
RewriteRule ^video videos.php [NC,L]
RewriteRule ^video?vid=([0-9]+) videos.php?q=$1 [NC,L]
RewriteRule ^article?num=([0-9]+) article.php?num=$1 [NC,L]
RewriteRule ^editorial?num=([0-9]+) editorial.php?num=$1 [NC,L]
RewriteRule ^news?news=([0-9]+) news.php?news=$1 [NC,L]
You cannot check against the query string in a rewrite rule. You need rewrite conditions for that:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2?
Demo here: http://htaccess.mwl.be?share=81e85c09-d505-5206-ab14-6c5059107808
If you want to actually redirect just add [R=301,L] to the end of the RewriteRule.
However, looking at the above I suspect you have your script sitting listening at /archive/index.php?data=foo&p=bar but want URLs to be like /archive/date/p, ie pretty.
This is actually a very common misconception about how htaccess URL rewrites work when you first get into them.
RewriteRules will mask or redirect URLs for you but they cannot change the underlying location a script is located at and thus the address used to pass it information.
In other words - you can mask /archive/index.php?data=foo&p=bar as /archive/date/p so that requests made to /archive/date/p resolve to /archive/index.php?data=foo&p=bar, but you cannot make it so that if you enter /archive/index.php?data=foo&p=bar as URL you have the URL change to /archive/date/p while still serving content from /archive/date/p. It has to be either or.
If this all sounds about right my advice would be as follows:
First, put your code into a different file, say /archive/script.php.
Next add the following to your htaccess:
RewriteCond %{QUERY_STRING} date=([^&]+)&p=(.+)
RewriteRule ^archive/? /archive/%1/%2? [R=301,L]
RewriteRule ^archive/([^/]+)/([^/]+) /archive/script.php?date=$1&p=$2
Note that the first two lines are the same as before, but now there is a new line that looks for the masked URL format of /archive/date/p and sends it off to the actual script, which is handled by the new RewriteRule.
The behaviour of the new rule is demoed here: http://htaccess.mwl.be?share=06751667-f16f-5c13-91eb-dd5cffdc6db3
Hope this makes sense / helps.

Display particular (dynamicaly) URL without parent folder. Leave The Others Alone

I started learning rewrites today
First of all here are examples of my links
Index.php?action=pictures
Index.php?action=pictures&type=kitchen
Index.php?action=pictures&type=ceiling
Index.php?action=services
Index.php?action=services&type=shower
Index.php?action=services&type=windows
In my .htaccess file I have this
RewriteEngine On
RewriteBase /
#Do not Rewrite files or folders
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC]
RewriteRule ^(.*?)$ $1 [L]
#Ordinary
RewriteRule ^\.htaccess$ .htaccess [F]
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(.*)/?$ Index.php?action=$1 [L]
Rewrites URLs to
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/services/shower
localhost/services/windows
//etc
I like to have ALL my links working as is with an exception of services where I don't need parent folder /services/.
Result:
localhost/pictures
localhost/pictures/kitchen
localhost/pictures/ceiling
localhost/services
localhost/shower
localhost/window
I tried to rewrite .htaccess but either I get only parentfolder to work or only subfolder.
I tried to add this but I do understand that this matches everything...
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]
I can however hardcode it like this
RewriteRule ^window/?$ Index.php?action=services&type=window [L]
Would like to have something dynamical. If folder services -> show no folder yet still be able to see localhost/services!
Is it possible?
Think of it this way: How would Apache know if a particular string is an action, or a type of service?
Well, you have three options:
We hardcode the types of services. Anything that does not match a type must be an action.
We hardcode the actions. Anything that does not match an action must thus be a type of service.
Apache has no way of knowing: We feed it to a script, who might be able to do some magic to find out what this string is.
In your case hardcoding the actions seems like the best idea, at least when the actions are static.
RewriteRule ^(.*)/(.*)/?$ Index.php?action=$1&type=$2 [L]
RewriteRule ^(pictures|services)/?$ Index.php?action=$1 [L]
RewriteRule ^(.*)/?$ Index.php?action=services&type=$1 [L]

Troubleshooting mod_rewrite in a .htacces with a LightSpeed

I have a couple web pages located at these locations:
Home Page / Index : www.codeliger.com/index.php?page=home
Education : www.codeliger.com/index.php?page=home&filter=1
Skills: www.codeliger.com/index.php?page=home&filter=2
Projects: www.codeliger.com/index.php?page=home&filter=3
Work Experience: www.codeliger.com/index.php?page=home&filter=4
Contact : www.codeliger.com/index.php?page=contact
I am trying to rewrite them to prettier urls:
codeliger.com/home
codeliger.com/education
codeliger.com/skills
codeliger.com/projects
codeliger.com/experience
codeliger.com/contact
I have confirmed that my htaccess file works and mod-rewrite works to google, but I cannot get my syntax working that was specified in multiple tutorials online.
RewriteEngine on
RewriteRule /home /index.php?page=home
RewriteRule /([a-Z]+) /index.php?page=$1
RewriteRule /education /index.php?page=home&filter=1
RewriteRule /skills /index.php?page=home&filter=2
RewriteRule /projects /index.php?page=home&filter=3
RewriteRule /experience /index.php?page=home&filter=4
How can I fix my syntax to rewrite these pages to prettier urls?
The first thing you should probably do is fix your regex. You cannot have a range like [a-Z], you can just do [a-z] and use the [NC] (no case) flag. Also, you want this rule at the very end since it'll match requests for /projects which will make it so the rule further down will never get applied. Then, you want to get rid of all your leading slashes. Lastly, you want a boundary for your regex, otherwise it'll match index.php and cause another error.
So:
RewriteEngine on
RewriteRule ^home /index.php?page=home
RewriteRule ^education /index.php?page=home&filter=1
RewriteRule ^skills /index.php?page=home&filter=2
RewriteRule ^projects /index.php?page=home&filter=3
RewriteRule ^experience /index.php?page=home&filter=4
RewriteRule ^([a-z]+)$ /index.php?page=$1 [NC]

mod_rewrite so that first-level subdirectory is a GET variable

Alright, title is REALLY sloppy.
Here's my problem: I have a news site and when you go to the main page (domain.com) it redirects you to domain.com/news/top?geography=San_Francisco after it figures out your geography.
How do I use the .htaccess so that it goes from domain.com/news/top?geography=San_Francisco domain.com/San_Francisco/news/top ?
There are some similar questions, but I have not found one similar enough in that you're editing the URL as a furtherback subdirectory.
It should also be noted that I am using the Code Igniter framework for PHP and it normally has it as domain.com/index.php/news/top?geography=San_Francisco but I did a mod_rewrite already to get rid of the index.php. The code is as follows for that:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Code I've tried:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
Before the index.php rule that you have, try adding this:
RewriteRule ^([^/]+)/news/top$ /news/top?geography=$1 [L,QSA]
You'll need to make sure the links you generate are in the form of domain.com/San_Francisco/news/top though.
But to take care of the links in the wild that still look like the old way, you have to match against the actual request:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /news/top\?geography=([^&]+)
RewriteRule ^news/top$ /%1/news/top? [L,R=301]
This will 301 redirect the browser if someone goes to the link domain.com/news/top?geography=San_Francisco and make it so the browser's address bar says this: domain.com/San_Francisco/news/top. At which point the browser will send another request for the second URL, and you use the rule above to change it back into the one with a query string.

Mod rewrite multiple languages

Alright im doing a site which is available in multiple languages. I have ran into some problems with my mod rewrite. I'm trying to do this:
Normal page without extra querystrings: /en/whatever
And the page with problems (with querystrings): /en/dashboard/project/projectid
And my .htaccess:
RewriteRule ^en/(.*)$ $1?lang=en [NC,L,QSA]
RewriteRule ^dashboard/project/([a-z0-9]+)$ projectdashboard.php?id=$1
But the ?lang=en is getting lost when opening this page: /en/dashboard/project/projectid
Use the QSA flag in both rules:
RewriteRule ^en/(.*)$ $1?lang=en [NC,QSA]
RewriteRule ^dashboard/project/([a-z0-9]+)$ projectdashboard.php?id=$1 [QSA]

Resources