Home > database >  Is there a way to create fake directories and assign them a value via .htaccess?
Is there a way to create fake directories and assign them a value via .htaccess?

Time:04-25

The goal is to create a "fake" language subdirectory after the HTTP_HOST following up with the REQUEST_URI.

[Issue] 
localhost/public_html causes 404, but localhost/public_html/index works.

Current code:

ErrorDocument 404 /public_html/404.php
ErrorDocument 500 /public_html/500.php
DirectoryIndex index.php
RewriteEngine On

RewriteRule ^$ /en/ [L,R=301]

# ASSIGN ENGLISH LANGUAGE PARAMETER IF NOT FOUND IN URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^(en|id|fr|ru|de|es|ro|tr|zh|ar)/ /en%{REQUEST_URI} [L,NC,R=302]

# ASSIGN LANGUAGE PARAMETER A VALUE IF FOUND IN URL
RewriteRule ^(en|id|fr|ru|de|es|ro|tr|zh|ar)/(.*) $2?language=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.] ?)/?$ $1.php [L,NC]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.] ?)/?$ $1.html [L,NC]

Folder directory:

xampp root folder
[
- public_html
]

website folder (public_html)
[
 - .htaccess
 - index.php
 - contact.php
 - style.css
 - etc.php
]

UPDATE: Thanks to anubhava the problem has been successfully solved!

CodePudding user response:

Make sure these 2 rules are your topmost rules:

ErrorDocument 404 /public_html/404.php
ErrorDocument 500 /public_html/500.php
DirectoryIndex index.php

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(index\.php)?$ /en/ [L,R=301]

# ASSIGN ENGLISH LANGUAGE PARAMETER IF NOT FOUND IN URL
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !^(en|id|fr|ru|de|es|ro|tr|zh|ar)/ /en%{REQUEST_URI} [L,NC,R=302]

# ASSIGN LANGUAGE PARAMETER A VALUE IF FOUND IN URL
RewriteRule ^(en|id|fr|ru|de|es|ro|tr|zh|ar)/(.*) $2?language=$1 [QSA,NC,L]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.] ?)/?$ $1.php [L]

RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^([^.] ?)/?$ $1.html [L]

Make sure to fully clean browse cache before testing this change.

  • Related