Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mod_rewrite select a folder for the domain

Situation

I'm using Zend framework, and thus attempt to call the folder by address ends in a fiasco and we gets the error: 'Invalid controller specified'. I needed to hook up additional forum to application in a separate folder. I change .htaccess file as follow:

RewriteRule ^forum(.*)$ forum$1 [L]

and for a while it was good, until I realized the fact that the page exists in two versions in two domains (php recognizes domain [.pl/.co.uk] and selects the language). So I decided to separate forums as a two separate modules (for example, the folder for Polish version forum will be: forum_pl, and for UK it will be: forum_uk - two different forums)

What's the problem?

It needs to rewrite .htaccess file to work like this:

IF: www.domena.pl/forum

THEN: open to the script from the folder /forum_pl

IF: www.domain.co.uk/forum

THEN: open to the script from the folder /forum_uk.

The worst thing is that when I tried to fix .htaccess file adding RewriteCond then the first redirect no longer work correctly, even after returning to the original (shown above) version.

Please help me and sorry if I did not find answers already given earlier.

EDIT:

First working solution

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]
RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]
RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]

It's look like it wants to work but, problems that needs solution is (TODO):

  1. When you type: www.domain.pl/forum it gets you to www.domain.pl/forum_pl/ but if you type: www.domain.pl/forum/ it gets you to www.domain.pl/forum/ (read from forum_pl). How to make that it works in first situation same, as in second.

EDIT:

My last solution:

#FORUM
#PL
RewriteCond %{REQUEST_URI} ^/forum$ [NC]
RewriteRule ^(.*)$ forum/ [R=301,L]

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]

RewriteCond %{REQUEST_URI} ^/forum_pl(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domena.pl/forum/ [R=301,L] #Dosen't work properly (?)

RewriteCond %{HTTP_HOST} ^www.domena.pl(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_pl/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_pl$1 [L]
#UK
RewriteCond %{REQUEST_URI} ^/forum_uk(.*)$ [NC]
RewriteRule ^(.*)$ http://www.domain.co.uk/forum/ [R=301,L] #Dosen't work properly (?)

RewriteCond %{HTTP_HOST} ^www.domain.co.uk(.*) [NC]
RewriteCond %{REQUEST_URI} ^/forum(/?)$ [NC]
RewriteRule ^forum[^/](.*)$ forum_uk/$1 #Dosen't work properly, but without it crash!
RewriteRule ^forum(.*)$ forum_uk$1 [L]

Final, working version:

RewriteRule ^forum_pl(.*)$ forum_pl$1 [L]
RewriteRule ^forum_uk(.*)$ forum_uk$1 [L]

RewriteRule ^forum_pl(.*)$ http://www.domena.pl/forum$1 [R=301,L] # Not working!
RewriteRule ^forum_uk(.*)$ http://www.domain.co.uk/forum$1 [R=301,L] # Not working!

# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]

# redirect to polish version of web under forum_pl if on .pl TLD and
#  request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum(.*)$ forum_pl$1 [L]

# redirect to english version of web under forum_en in on .uk TLD and
#  request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum(.*)$ forum_uk$1 [L]

It is a pity that you can call from your browser forum_uk and forum_pl folders manually.

like image 300
MrTomAsh Avatar asked Nov 07 '12 15:11

MrTomAsh


1 Answers

Not sure if I oriented correctly in your long question with some evolution :-) Lets try this:

# Normalize URL first:
RewriteRule ^forum$ forum/ [R=301,L]

# redirect to polish version of web under forum_pl if on .pl TLD and
#  request is made to /forum/ (already normalized)
RewriteCond %{HTTP_HOST} =www.domena.pl [NC]
RewriteRule ^forum/(.*)$ http://www.domena.pl/forum_pl/$1 [R=301,L]

# redirect to english version of web under forum_en in on .uk TLD and
#  request is made to /forum/
RewriteCond %{HTTP_HOST} =www.domain.co.uk [NC]
RewriteRule ^forum/(.*)$ http://www.domain.co.uk/forum_uk/$1 [R=301,L]

If you need the rewites to act differently, let me know

like image 176
Kamil Šrot Avatar answered Nov 09 '22 18:11

Kamil Šrot