I have domain example.com, when someone go to this domain, I need to automatically redirect to example.com/en/ and need /en/ to be a parameter of variable lang.

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)?/?$ ?lang=$1&menu_url=$2 [L]
  • What is an example of a value of the menu_url parameter? Have you tried to implement a redirect rule that redirects when you hit the base URL? Mar 2 '19 at 4:11
  • Normal url is example.com/index.php?lang=en&menu_url=contact It normaly works fine. But i need automaticaly load example.com/en/ instead of example.com. I tried to redirtect via RedirectMatch, but it's not working with parametr. example.com/en/ is depend on loading menu language. It's easy, i need to disable load only example.com, i need only example.com/en/ Mar 2 '19 at 7:34
  • Your question is still not clear. Redirecting to /en/ and including en in the URL parameter are seemingly two different tasks. Is the directive you posted a working directive for passing the lang (and menu_url) URL parameter (although it's not strictly correct, if it is)? You need to edit your question to list examples of the URLs being requested and the desired target URL and underlying filesystem path. What URLs are you linking to in your application? For instance, what about requests for example.com/foo? Or are you only dealing with example.com/, as suggested in your question?
    – MrWhite
    Mar 2 '19 at 12:15

Your question is not clear. However, to redirect from example.com/ to example.com/en/ (as stated in your question) then you would need something like the following at the top of your .htaccess file:

RewriteEngine On

RewriteRule ^$ /en/ [R,L]

That literally only redirects from / to /en/. However, if you have a requested URL of the form /foo that need to redirect to /en/foo then you would need something like the following instead:

RewriteRule !^en/ /en%{REQUEST_URI} [R,L]

I assume you are already linking to /en/... in your application and that you are using Apache 2.4 (as opposed to 2.2) - otherwise the above directives may not work correctly.

RewriteRule ^([^/]+)/([^/]+)?/?$ ?lang=$1&menu_url=$2 [L]

Then your existing directive (above) would rewrite the request to include the lang URL parameter. However, this is not strictly. If you are intending to rewrite the request to index.php (as mentioned in your comment), then you should explicitly include this inthe substitution string, instead of reply on mod_dir to issue an additional subrequest for the directory index. In other words:

 RewriteRule ^([^/]+)/([^/]+)?/?$ index.php?lang=$1&menu_url=$2 [L]
  • Thanks for answer, finally i use part of your sentence and think, how to do. In the end, i used php header with requirement and it's absolutely, what i want and what i find. Mar 2 '19 at 16:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.