user2519032
user2519032

Reputation: 849

.htaccess all directories give 403 Forbidden

I'm using .htaccess on my multilingual website to make some URL modifications for nice SEO.

By default all the folders are restricted with 403 Forbidden, but I want to make exceptions on two paths. /de and /en. Yes, these paths are for multilingual and when I have pages like:

www.mywebsite.com/en/page

It's working properly, but when I try to return to the main page with link like this:

www.mywebsite.com/en

it shows 403 forbidden.

Here's my full code:

RewriteEngine on

#remove .php
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

#redirect lang
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L]


RewriteCond %{REQUEST_URI} !/en

On the last line as you can see I've tried to allow access to /en, but it doesn't work.

How can I allow the access of these paths: /en and /de ?

EDIT:

After lot of searching, I've found that the main website on the same server that's made on Drupal, it has own .htaccess file and that's why it shows 403 forbidden on the new website (custom cms) that is located on a separate folder inside. But when I delete the .htaccess from the main website as a test, now on the new website when I enter www.mywebsite.com/en/ it shows 404 not found.

That doesn't happen if I enter www.mywebsite.com/en

It simply redirects to the index page.

After double check I can see the language switcher on my website automatically change the languages, so if page was /team, it acts like /en/team and on the front page it acts like this /en/, not /en.

EDIT2

I've followed the w3dk answer, but it still shows the same error. This is the full code that I'm trying out:

RewriteEngine on

#remove .php
#RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

RewriteRule ^([^\.]+)/?$ $1/index


#redirect lang
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/(.*)$ $2.php?lang=$1 [L]

This line RewriteRule ^([^\.]+)/?$ $1/index breaks the language switcher functionality. For an example /de/team doesn't change to the German content. Right now I've commented that line, 'cos its not working properly.

I'm posting the code from my language switcher too:

<?php
$currentURL = basename($_SERVER['REQUEST_URI']);
?>
        <ul class="language-switcher">
            <li><a href="/de/<?php echo $currentURL; ?>">DE</a></li>
            <li><a href="/en/<?php echo $currentURL; ?>">EN</a></li>
        </ul>   

If I enter this page

www.mywebsite.com/en/team

and after that click on the language switcher on German version, it successfully shows the German content and the URL is like this:

www.mywebsite.com/de/team

BUT if I go to the front page that has this URL:

www.mywebsite.com

and click on the language switcher on German version the URL becomes like this and it shows 403 or 404 page:

www.mywebsite.com/de/

Upvotes: 2

Views: 6749

Answers (1)

MrWhite
MrWhite

Reputation: 45968

I want /en to load the index page /en/index

Try adding the following after your first rule:

RewriteRule ^([^\.]+)/?$ $1/index

This should then be caught again by the final rule that ultimately rewrites the request to index.php?lang=en.

Alternatively, try the following:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\.]+)/?$ index.php?lang=$1 [L]

Upvotes: 2

Related Questions