3

I have Grav CMS based multilanguage site and two domains: grav-site.pl and grav-site.com

I want to redirect :

  1. grav-site.pl/en -> grav-site.com
  2. grav-site.com/pl -> grav-site.pl

In grav knolage base I found solution which should work so I add this to my .htaccess

## Begin - Multilanguage Setup
RewriteRule ^en/?$ "http://grav-site.com" [R=301,L]
RewriteRule ^pl/?$ "http://grav-site.pl" [R=301,L]

# handle sub pages, exclude admin path
RewriteCond %{REQUEST_URI} !(admin) [NC]
RewriteRule ^en/(.*)$ "http://grav-site.com/$1" [R=301,L]
RewriteCond %{REQUEST_URI} !(admin) [NC]
RewriteRule ^pl/(.*)$ "http://grav-site.pl/$1" [R=301,L]
# End - Multilanguage Setup

but when I go to http://grav-site.com/pl nothings happend (there is no redirection) and CMS engine remove /pl

How to change .htaccess entries to achive proper redirection?

P.S. rest of .htaccess https://github.com/getgrav/grav/blob/develop/webserver-configs/htaccess.txt

  • "rest of .htaccess" - But where have you put these new directives in relation to your existing directives? Do both domains point to the same place? – MrWhite Jun 1 '17 at 15:02
  • I add entries from post above after <IfModule mod_rewrite.c>...</IfModule>, just on end of file. – user19291 Jun 2 '17 at 7:28
  • Well, that's at least part of your problem: these redirects would need to go near the start of the file, before your existing rewrites - otherwise, they will never get processed. (In fact, the order of some of those other directives doesn't look quite right... for instance, the "security" block should appear near the start of the file, before the front controller?) – MrWhite Jun 2 '17 at 7:50
  • 1
    @user82217 you are right. On web page that I linked with solution I found there is no info about where redirect entries should be added, but when I move them just after <IfModule mod_rewrite.c> it works. Thanks, but there is no answer to accept. – user19291 Jun 2 '17 at 14:20
1

I add entries from post above after <IfModule mod_rewrite.c>...</IfModule>, just on end of file

This would seem to be the main cause of your problem. These directives (redirects) would need to go near the start of the file, before your existing rewrites - otherwise, they will never get processed.

The "front controller" (in the middle of the file that begins ## Begin - Index) captures all requests and routes them through index.php (your "front controller"), any mod_rewrite directives that follow this are unlikely to get processed, unless the request is for a physical file.

As a general rule, external redirects should always go before internal rewrites.

For the same reason I would question the location of your "security" directives (in the block that starts ## Begin - Security), which also follows your front controller. This might actually be OK - as they only get processed for files which exist. However, again as a general rule, blocking/security directives would normally go near the top of the file (even before the redirects).

| improve this answer | |
0

Uncomment the RewriteBase line in your .htaccess file which is https://github.com/getgrav/grav/blob/develop/webserver-configs/htaccess.txt

How to redirect "www.example1.com" to "www.example2.com"

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example1.com [NC]
RewriteRule ^(.*) http://www.example2.com/$1 [L,R=301]

Redirect from "www.example.com" to "example.com" and vise versa

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]

and vise versa

RewriteEngine On 
RewriteBase / 
RewriteCond %{HTTP_HOST} ^example.com [NC] 
RewriteRule ^(.*) http://www.example.com/$1 [L,R=301]

Redirect from all nonexistent subdomains to the website:

RewriteEngine On 
RewriteBase / 
RewriteCond %{HTTP_HOST} ^(.*).example.com [NC] 
RewriteRule ^(.*)$ http://example.com/$1 [L,R=permanent]

Redirect all the website requests to specific web page:

RewriteEngine On 
RewriteBase / 
RewriteRule ^(.*)$ http://example.com/desired_page/ [L,R=permanent]
| improve this answer | |
  • "Uncomment the RewriteBase line" - Why is that? (None of the rules/redirects you've posted appear to make use of the RewriteBase?!) – MrWhite Jun 1 '17 at 23:32
  • the answer was edited. check your github.com/getgrav/grav/blob/develop/webserver-configs/… to find the string with RewriteBase rule and simply uncomment it deleting # symbol. – Mr. Raspberry Jun 2 '17 at 7:00
  • I appreciate your help but after uncommenting RewriteBase nothings changed. Here is full .htaccess (with change) for convenience pastebin.com/CBdH82wN – user19291 Jun 2 '17 at 7:36
  • Try to put the strings inside the IfModule tag. – Mr. Raspberry Jun 2 '17 at 7:44

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.