Jump to content

Managing Two .htaccess Files for Multilingual Website with Shared Root Content


AndreiRoman

Recommended Posts

 

Hello, I need some help with managing .htaccess files for my multilingual website.

Problem Description:

I have a website that supports two languages: Romanian and English. The primary domain is for Romanian content, and the secondary domain (subdomain) is for English content. All of the content (HTML, PHP, etc.) is stored in the root directory, which serves both versions of the website.

I want to use two .htaccess files:

The main .htaccess file in the root directory should handle rules for the Romanian version of the website (for the primary domain website.ro).

The secondary .htaccess file is located in the /en/ folder and should handle specific rules for the English version of the website (for the secondary domain en.website.ro).

Requirements:

I want the English version (en.website.ro) to use the same content and files as the Romanian version (i.e., everything from the root directory), but it should apply the specific rules from the /en/.htaccess file.

The Romanian version (website.ro) should continue to work normally, using the .htaccess rules defined in the root directory.

How can I ensure that the /en/.htaccess file only applies to the en.website.ro domain while still using the files from the root directory?

Thank you,

Andrei

Link to comment
Share on other sites

.htaccess files are for people who can't modify server configuration. Can you? If you can then you should be putting stuff into the VirtualHost definitions - you'll get slightly better performance that way. And it would also solve your problem as you'd be putting the Romanian rules in the main site config and the English rules in the en subdomain site config.

Link to comment
Share on other sites

15 hours ago, requinix said:

.htaccess files are for people who can't modify server configuration. Can you? If you can then you should be putting stuff into the VirtualHost definitions - you'll get slightly better performance that way. And it would also solve your problem as you'd be putting the Romanian rules in the main site config and the English rules in the en subdomain site config.

Unfortunately, I do not have access. I've also tried the solution with one single .htacces file(in the root) but it is not working either: 

 

RewriteCond %{HTTP_HOST} ^mobirom.ro
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule (.*) https://www.mobirom.ro/$1 [R=301,L]


RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(2)\.html$ /mese/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(6)\.html$ /scaune-curbate/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(3)\.html$ /mese-bar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(5)\.html$ /scaune-bar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(4)\.html$ /tabureti/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(7)\.html$ /seturi-mese-si-scaune/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(15)\.html$ /mobilier-lemn-curbat/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(9)\.html$ /masute-cafea/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(11)\.html$ /scaune-balansoar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(13)\.html$ /mobilier-terasa/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(1)\.html$ /scaune/$1_$2_$3.html [L,R=301]

RewriteCond %{HTTP_HOST} ^en\.mobirom\.ro
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule (.*) https://en.mobirom.ro/$1 [R=301,L]

RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(2)\.html$ /tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(6)\.html$ /bent-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(3)\.html$ /bar-tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(5)\.html$ /bar-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(4)\.html$ /stools/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(7)\.html$ /table-chair-sets/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(15)\.html$ /bent-wood-furniture/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(9)\.html$ /coffee-tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(11)\.html$ /rocking-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(13)\.html$ /outdoor-furniture/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(1)\.html$ /chairs/$1_$2_$3.html [L,R=301]
 

it seems that it it taking in the consideration the rules from the root only..

 

Thank you 

Link to comment
Share on other sites

The server should be configured as:

- en.mobirom.ro uses that /en/ directory as its root
- the English URL paths do not have an "/en/" prefix, as in "/en/whatever/page.html" - they should be the same as the normal Romanian ones

Doing a little checking on your site, that's not the case: the English site is also using the root directory, which means I have to go to en.mobirom.ro/en for it to work.

After fixing the site to use /en/ as its root, and hopefully you can but if not then keep reading and we'll solve that problem separately,

- The .htaccess for the Romanian site (/.htaccess) will only apply to mobirom.ro
- The .htaccess for the English site (/en/.htaccess) should only apply to en.mobirom.ro, however:
- Unless you tell Apache otherwise, mobirom.ro/en/whatever/page.html will work. This is bad for SEO and you should make sure the English site is only accessible at en.mobirom.ro. That will also mean the /en/.htaccess only gets used for the English site.
- This thread is public to the internet. Please don't create debugging pages like you have now for /en/ unless you want anybody to be able to see them... Checking the REMOTE_ADDR to only allow you would solve this.

So /.htaccess should look something like

RewriteEngine on

#
# HTTP->HTTPS and WWW redirect
#

RewriteCond %{HTTPS} off
RewriteRule ^ https://www.mobirom.ro%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !=www.mobirom.ro
# you should not need these two...
#RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
#RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
# you do not need this, Let's Encrypt will follow redirections
# they also won't validate the SSL cert, so an expired or even self-signed cert will be okay
#RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^ https://www.mobirom.ro%{REQUEST_URI} [L,R=301]

#
# English site redirect
#

RewriteRule ^en/(.*) https://en.mobirom.ro/$1 [L,R=301]

#
# HTML pages
#

RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(2)\.html$ /mese/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(6)\.html$ /scaune-curbate/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(3)\.html$ /mese-bar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(5)\.html$ /scaune-bar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(4)\.html$ /tabureti/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(7)\.html$ /seturi-mese-si-scaune/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(15)\.html$ /mobilier-lemn-curbat/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(9)\.html$ /masute-cafea/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(11)\.html$ /scaune-balansoar/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(13)\.html$ /mobilier-terasa/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(1)\.html$ /scaune/$1_$2_$3.html [L,R=301]

And /en/.htaccess will look similar:

RewriteEngine on

#
# HTTP->HTTPS and WWW redirects
#

RewriteCond %{HTTPS} off
RewriteRule ^ https://en.mobirom.ro%{REQUEST_URI} [L,R=301]

RewriteCond %{HTTP_HOST} !=en.mobirom.ro
# you should not need these two
#RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
#RewriteCond %{REQUEST_URI} !^/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
# you do not need this, Let's Encrypt will follow redirections
# they also won't validate the SSL cert, so an expired or even self-signed cert will be okay
#RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteRule ^ https://en.mobirom.ro%{REQUEST_URI} [L,R=301]

#
# HTML pages
#

RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(2)\.html$ /tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(6)\.html$ /bent-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(3)\.html$ /bar-tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(5)\.html$ /bar-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(4)\.html$ /stools/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(7)\.html$ /table-chair-sets/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(15)\.html$ /bent-wood-furniture/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(9)\.html$ /coffee-tables/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(11)\.html$ /rocking-chairs/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(13)\.html$ /outdoor-furniture/$1_$2_$3.html [L,R=301]
RewriteRule ^([a-zA-Z0-9-]+)_(\d+)_(1)\.html$ /chairs/$1_$2_$3.html [L,R=301]

Since your server looks properly configured for the two sites, you could actually use %{SERVER_NAME} in place of the hostnames, making some of this easier to copy and paste.

One more thing: PHP 7.4 is really old so please upgrade if you can.

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.