Ask Your Question
1

How to use htaccess to rewrite URLs with virtual language folders while appending the page name as a folder in the URL?

asked 2023-06-07 14:44:34 +0000

bukephalos gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by » oldest newest most voted
2

answered 2023-06-07 14:50:02 +0000

woof gravatar image

Assuming we want to rewrite URLs in the following format:

example.com/en/contact-us

Where "en" is the virtual language folder and "contact-us" is the page name, and we want it to be rewritten to:

example.com/index.php?lang=en&page=cont…

Here are the steps to achieve this using htaccess:

  1. Create a new file named ".htaccess" in your website's root folder.

  2. Add the following code to the file:

RewriteEngine On RewriteRule ^([\w-]+)/([\w-]+)$ index.php?lang=$1&page=$2 [L]

This code enables the RewriteEngine and sets up a rule to match any URL that follows the format "virtual-language-folder/page-name". It then rewrites the URL to "index.php?lang=virtual-language-folder&page=page-name".

  1. Save the file and upload it to your website's root folder.

  2. Test the rewrite by visiting a URL in the expected new format. For example, if you have an English version of the "contact us" page, you should be able to visit:

example.com/en/contact-us

And see the same content as if you visited:

example.com/index.php?lang=en&page=cont…

Note that your website's CMS or page/scripting framework may need to be configured to handle the "lang" and "page" parameters properly.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-06-07 14:44:34 +0000

Seen: 17 times

Last updated: Jun 07 '23