English and Spanish friendly URLs in PHP and htaccess
P粉969253139
P粉969253139 2023-09-08 10:17:27
0
1
508

I have a website in English and Spanish, each language has its own folder, for example:

news.com/en/ for English
news.com/es/ for Spanish

The latest news list is displayed at:

news.com/en/latest-news.php for English
news.com/es/ultimas-noticias.php for Spanish

When a user clicks on an article to read the entire article, they are linked to read the entire article from here (using a slug stored in MySQL), like this (for example):

news.com/en/general/news/europe/2022/10/22/this-is-a-nice-title

All articles use the following page to get slugs:

news.com/en/read-article.php for English
news.com/es/leer-noticia.php for Spanish

On these pages, the script queries MySQL to search for slugs and displays the full article. The problem is that for some reason I don't understand I'm not getting any results.

If I move the file to the root directory:

news.com/read-article.php for English
news.com/leer-noticia.php for Spanish

Then it works fine, but adding these files into subfolders I don't get any results.

Importantly, slugs are saved in MySQL like this:

general/news/europe/2022/10/22/this-is-a-nice-title

Because they look better than dashes and the date is the date it was published, it will change for each post.

This is the .htaccess code I created for these subfolders:

Update: The problem is only in htaccess and even though I answered my own question, it's still not completely resolved

RewriteEngine On

RewriteRule ^(en)/([a-zA-Z0-9-]+)$ /read-article.php?id= [L]

RewriteRule ^(es)/([a-zA-Z0-9-]+)$ /leer-noticia.php?id= [L]

Do you know where the problem lies?

P粉969253139
P粉969253139

reply all(1)
P粉281089485

For those looking for help as they have the same problem and I'm sure there are many:

I was able to fix it in a way with great luck, but certainly not a professional way, it might be the wrong way, but it works. There are many similar questions on this website without answers, so it seems that the average person does not know much about htaccess.

The solution I found is:

RewriteEngine on
RewriteRule ^en/([a-zA-Z0-9-/]+)$ /en/read-article?slug=  [L]
RewriteRule ^en/([a-zA-Z0-9-/]+)$ /en/leer-noticia?slug=  [L]

So you can have a website in multiple languages, each language in a subfolder, but even if you only run a single language website and use subfolders, then you will definitely need this help, just Run this .htaccess and it will work (edit with your filename obviously)

Now we definitely need to read more about [L] in the code, there are many other similar codes:

I don't understand the following commands, if you need them, but you can definitely find it if you ask for help, or maybe some people here can share the knowledge with us:

DirectoryIndex home.php  
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
[L]
Options +FollowSymLinks -MultiViews
[QSA]

No one else has answered this question, it's the same as most questions about .htaccess and friendly URLs, but there's at least some unprofessional stuff here, but anyway I'm sharing some noob knowledge because it's for this community Main idea, not everyone is a professional but we can all share knowledge. If there's a better answer, I'll be sure to mark it as correct

Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template