htaccess multi language site with sub directories, and default 301 - .htaccess

I am having some issues setting up my htaccess to allow multiple languages utilising the sub directory method eg:
http://www.domain.com/en/
http://www.domain.com/sw/
http://www.domain.com/ie/
Also to complicate things, the project isn't currently live, its on a dev server. For example, I am currently accessing the project at:
http://dev.domain.com/devname/projectname/
And I want the above to automatically 301 redirect to:
http://dev.domain.com/devname/projectname/en/
Here is my htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
# ----------------------------------------------------------------------
# MULTI LANGUAGE SUB DIRECTORY
# ----------------------------------------------------------------------
RewriteCond %{REQUEST_URI} !^/(en|sw)/
RewriteRule ^(.*)$ en/$1 [R=301,L]
# ----------------------------------------------------------------------
# Rewrite rules
# ----------------------------------------------------------------------
## CASE STUDIES ##
RewriteRule ^casestudies/([^/\.]+).html$ index.php?controller=contents&method=viewCasestudy&link=$1 [L,QSA]
## PRODUCTS ##
RewriteRule ^products/([^/\.]+).html$ index.php?controller=contents&method=viewProduct&link=$1 [L,QSA]
RewriteRule ^([a-z{2}]+)(/)?$ index.php?controller=contents&method=viewHome&lang=$1 [L,QSA] # Default load
RewriteRule ^(/)?$ index.php?controller=contents&method=viewHome [L,QSA] # Default load
The above will actually redirect to:
http://dev.domain.com/home/webserver_dir/devname/projectname/en/
..and if I use RewriteBase it seems to just goto...
http://dev.domain.com/en/
So my question: How do I get the language URLs working correctly relative to the directory its in on my dev server, and then ideally will work when it goes live without any environment specific rules.
Bonus question: Do I need to add the ([a-z{2}]+) bit in front of all my subsequent rewrite rules or can I have a catch all that will effect all further rules?
EDIT -----------------------------
I have reduced it down to the following as suggested...
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /devname/projectname/
RewriteCond %{REQUEST_URI} !^/(en|sw)(/|$) [NC]
RewriteRule ^(.*)$ en/$1 [R=301,L]
RewriteRule ^([a-z]{2})/?$ index.php?controller=contents&method=viewHome&lang=$1 [NC,L,QSA] # Default load
... but now its redirecting to http://dev.domain.com/devname/projectname/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/en/, any ideas?

Have you tried the answer in the following link? It should do what you're trying to achieve.
Endless Redirect Loop by htaccess rules multi language
RewriteEngine On
RewriteBase /
# empty url -> redirect to en/
RewriteCond %{QUERY_STRING} !lang=(en|de)
RewriteRule ^$ en/ [R=301,L]
# url is ONLY '/en' or '/de' -> redirect to /en/ or /de/ (adding slash)
RewriteRule ^(en|de)$ $1/ [R=301,L]
# now all urls have en/ de/ -> parse them
RewriteRule ^(en|de)/(.*)$ $2?lang=$1&%{query_STRING} [L]

If .htaccess must not change
Change your <VirtualHost> configuration for your DEV server project as
<VirtualHost *:80>
ServerName dev.domain.com
ServerAlias project.domain.com
DocumentRoot "/home/webserver_dir/devname/projectname"
</VirtualHost>
These changes would typically go in your httpd-vhosts.conf file. Your .htaccess files would now have
RewriteBase /
to mark root as your base directory for both your development and live servers.
If you're trying to version your projects or test multiple projects on the same dev host, then you would have to incorporate the naming scheme into the domain names instead of the URL path. For example,
<VirtualHost *:80>
ServerName dev1.domain.com
ServerAlias project1.domain.com
DocumentRoot "/home/webserver_dir/dev1/project1"
</VirtualHost>
<VirtualHost *:80>
ServerName dev2.domain.com
ServerAlias project2.domain.com
DocumentRoot "/home/webserver_dir/dev2/project2"
</VirtualHost>
The bottom line is that you can not have the same .htaccess file rules working untouched with different deployment directories unless you resort to mod-rewrite way of if-else mumbo jumbo which would just be added clutter once you've gone live.
For the rules to work transparently, Apache must only see and apply the rules on what's going live (the content that comes after /devX/projectX/ directories) which is what shifting the DocumentRoot does here for us.
If minimal mods to .htaccess are okay
Not everyone has access to Apache's .conf files. Certain hosts out-rightly reject requests to modify them. Which is why, if they have at least kept mod-rewrite enabled, a lot of website's settings can be tinkered with. One of them is to use RewriteBase to handle the different deployment directories.
So, if you keep RewriteBase / on live but change it to RewriteBase /devX/projectX/ for development, most of your RewriteRules should work as is. So, /devname/projectname/ should correctly redirect to /devname/projectname/en/.
Your use of ([a-z{2}]+) is incorrect. You probably meant ([a-z]{2}) to capture exactly two letters. If you meant to capture two or more, it would become ([a-z]{2,}). So, your default load rewrite would become
RewriteRule ^([a-z]{2})/?$ index.php?controller=contents&method=viewHome&lang=$1 [NC,L,QSA] # Default load
You're correct to assume that you would need this regex for all subsequent rules or they would fail to match. So, your RewriteRule for casestudies won't work. A simpler way to not care about the language prefix is to drop the ^ start of URL path anchor as
RewriteRule /casestudies/([^/\.]+).html$ index.php?controller=contents&method=viewCasestudy&link=$1 [NC,L,QSA]
RewriteRule /products/([^/\.]+).html$ index.php?controller=contents&method=viewProduct&link=$1 [NC,L,QSA]
Your last RewriteRule matching ^(/)?$ isn't required because you're already doing a 301 redirect for all URLs with no language directory prefix to /en/$1 above, which should ideally be
RewriteCond %{REQUEST_URI} !^/(en|sw)(/|$) [NC]
RewriteRule ^(.*)$ en/$1 [R=301,L]
Otherwise, /en would get redirected as well to /en/en.

Related

Redirect rewritten URL to a new rewritten URL

I'm working on my website link architecture. I would like to redirect with my .htaccess already rewritten URL to a new one to keep my incoming links active.
www.website.com/profile-info/ to www.website.com/profile/
This is my actual working .htaccess :
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_URI} /profile-info/?$ [NC]
RewriteRule . models.php [L]
So in order to redirect /profile-info/ to the new URL : /profile/ . I end up with this code. However it redirects /profile/ to /profile-info/.
RewriteCond %{REQUEST_URI} /profile/?$ [NC]
RewriteRule . /profile-info/ [L,R=302]
I think this is what you are looking for:
RewriteEngine On
RewriteMap /
RewriteRule ^/?profile-info/?$ /profile [END,NE,R=301,QSA]
RewriteRule ^/?profile/?$ models.php [L,QSA]
It is a two step strategy:
redirect "old" links pointing to /profile-info to the new /profile
internally rewrite /profile to models.php
You may have to change the RewriteMap, this obviously depends on your situation.
And a general hint: you should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server, often without reason. They are only provided for situation where you do not have access to the host configuration (read: really cheap hosting providers) or in case an application needs to write its own rules (which is an obvious security nightmare...).
You can use these 2 rules:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# redirect /profile-info to /profile
RewriteRule ^profile-info/?$ /profile [R=301,NE,L]
# rewrite /profile to /models.php
RewriteRule ^/?profile/?$ models.php [L,NC]

Symfony2 on GoDaddy Shared with multiple sites

I'm close to getting Symfony2 working a GoDaddy shared hosting plan (with multiple sites).
So I have the following accessible via web/ftp (note, there is another directory level above this I can access over SSH):
/ <-- dummy domain points here to "hide" it sort of
----/SiteA <-- sitea.com (Wordpress, working fine)
----/SiteB <-- siteb.com (Wordpress, working fine)
----/SiteC <-- sitec.com (Wordpress, working fine)
----/Symfony
--------/app
--------/web <-- symfonysite.com
------------/.htaccess
------------/app.php
I can access the default controller at symfonysite.com just fine. But when I click on a link (example symfonysite.com/my/page), I get a 500 error.
When I try going to symfonysite.com/app.php it's even weirder. I end up on symfonysite.com/Symfony/web.
Clearly something is not right in my .htaccess, because it is not rewriting properly. Somehow it is relative to web root, not to the root of the particular parked domain. I'm guessing this is an easy fix, but I'm not experienced with .htaccess (this is just the default from 2.3). Here it is below, with comments removed:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
As I was posting, I figured out the answer by reading the comments in .htaccess more closely. Posting this Q&A in case it helps others.
The problem is here:
# Determine the RewriteBase automatically and set it as environment variable.
# If you are using Apache aliases to do mass virtual hosting or installed the
# project in a subdirectory, the base path will be prepended to allow proper
# resolution of the app.php file and to redirect to the correct URI. It will
# work in environments without path prefix as well, providing a safe, one-size
# fits all solution. But as you do not need it in this case, you can comment
# the following 2 lines to eliminate the overhead.
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
I believe that is changing my rewrites to be /Symfony/web/app.php instead of /app.php. Commenting out those lines worked.

URL-rewriting with index in a "public" folder

I’m a newcomer in the development world. I desperately try to get the good URL. I checked the site for similar problems but I can’t find exactly what I need. Or I do it badly.
Here is the situation: I set up a project for a site whose the index.php file is in a folder named Public.
To be clearer, here is the URL I have now to reach the homepage of the built site:
http:// Domain Name.com/ Folder / Name of the site/public
My concern is about the folder Public: I don’t want it appears in the URL.
Here is the URL I’d like to get:
http:// Domain Name.com/ Folder / Name of the site
In fact, I’d like this URL permits to get the index file placed in the folder "Public".
I can’t access the Apache configurations (shared host) so I have a .htaccess I placed in the project (i.e: www/ Folder /Name of the site /.htaccess). Here is its content:
Options -Indexes +FollowSymLinks +Multiviews
RewriteEngine On
RewriteBase /public/
RewriteRule ^(.*)$ index.html [NC,L]
I made something very simple for now because I tried lots of things without efficient result.
Not really sure what you are trying to do, but if you want to remove the /public/ path that appears in the URL, you need to remove it from all your links, second, turn off multiviews, it's not what you want, third, you need a rule to externally redirect the browser when a request is made for /public/, then you need to internally rewrite requests to point to public.
Options -Indexes +FollowSymLinks -Multiviews
RewriteEngine On
# externally redirect, must match against %{THE_REQUEST}
RewriteCond %{THE_REQUEST} ^(GET|HEAD|POST)\ /public/
RewriteRule ^/?public/(.*)$ /$1 [L,R=301]
# internally rewrite it back, but we must first check that it's pointing to a valid resource:
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -d [OR]
RewriteCond %{DOCUMENT_ROOT}/public%{REQUEST_URI} -s
RewriteRule ^(.*)$ /public/$1 [L]

rewrite rule for my drupal site url not working

I'm trying to write a couple of rewrite rules for my drupal site in its .htaccess file and it doesn't seem to work at all. My lack of prior knowledge and experience in regex and mod_rewrite doesn't seem to help the cause either.
The URLs I'm trying to rewrite are as follows
Source:
http://www.mydrupalsite.com/somealphanumericwithspecialchars/somealphanumericwithspecialchars/somealphanumericwithspecialchars/somealphanumericwithspecialchars/Keyword1/Keyword2/UNIQUEID?queryparam1=somenumber
Destination:
http://www.mydrupalsite.com/legacystuff/UNIQUEID
My .htaccess mod_rewrite code is
RewriteEngine on
# Rewrite URLs of the form 'x' to the form 'index.php?q=x'.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
#This is the only rule written by me others are drupals
RewriteRule ^(.*)/(.*)/(.*)/(.*)/Keyword1/Keyword2/([0-9]*)?$ legacystuff/content/$5 [R=302]
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
My understanding and implementation based on some quick browsing on tutorials was the first four (.*) would match with the four alphanumericspecialchars in the URL, keyword1 and keyword2 would remain constant so i could match them as such, the unique numeric id which is actually required for my destination URL (param $5 in the destination) would be captured by ([0-9*])?. I use the following vhost in my httpd.conf of my local environment
<VirtualHost *:80>
DocumentRoot "C:/wamp/www/mydrupalsite"
ServerName mydrupalsite.com
</VirtualHost>
My trouble is that the redirect from the source to destination URL does not occur after restarting my local server and clearing the cache. Any help on where I went wrong and how to make it right would be really appreciated.
Finally got it working, added the RewriteBase for my virtual host setting and placed my RewriteRule underneath it with the trailing ? in my destination to ignore query string and the L flag. Setting RewriteLog in my httpd.conf helped understand things from a better perspective. Hopefully this helps someone else new to .htaccess in drupal.
RewriteBase /
RewriteRule ^(.*)/(.*)/(.*)/(.*)/Keyword1/Keyword2/([0-9]*)?$ legacystuff/content/$5? [R=301,L]

Point External Domain and Sub Directories to Site's Folder

A lot of pages in our site uses an old subdomain for images, css and javascript files. I inherited the project from newbie developers and they didn't use any common design template. The result are hundreds of pages with static references to contents an examples are:
http://static.foo.bar/css/sample.css
http://static.foo.bar/brochure/css/sample.css
http://static.foo.bar/company/newsletter/js/common.js
http://static.foo.bar/images/version2/header.jpg
...and hundreds of other locations. I need to point them all to the main domain instead of the subdomain without creating rules for each of these on the .htaccess file. So:
http://static.foo.bar/css/sample.css
should point to:
http://www.foo.bar/css/sample.css
http://static.foo.bar/brochure/css/sample.css
should point to:
http://www.foo.bar/brochure/css/sample.css
http://static.foo.bar/company/newsletter/js/common.js
should point to:
http://www.foo.bar/company/newsletter/js/common.js
http://static.foo.bar/images/version2/header.jpg
should point to:
http://www.foo.bar/images/version2/header.jpg
I know this is possible only I'm not a server guy. Any help would be very much appreciated.
Create an .htaccess file in the document root with this content:
Add this to your server configuration, %{HTTP_HOST} check does not work from .htaccess:
<IfModule mod_rewrite.c>
# bad matching: RewriteCond %{HTTP_HOST} !^static\.foo\.bar [NC]
#
# correction, matching everything other than www.foo.bar :
RewriteCond %{HTTP_HOST} !^www\.foo\.bar$ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/(.*) http://www.foo.bar/$1 [L,R]
</IfModule>
See http://httpd.apache.org/docs/2.0/misc/rewriteguide.html#url
UPDATE:
Giving it a second though, if it's hosted on the same server, just add
ServerAlias static.foo.bar
to the www.foo.bar configuration to serve up the static content, too.
UPDATE #2 based on feedback:
This should work (works on my pc) in the .htaccess file, this redirects all requests coming to static.foo.bar/* to www.foo.bar/* :
RewriteCond %{HTTP_HOST} ^static\.foo\.bar [NC]
RewriteRule ^(.*)$ http://www.foo.bar/$1 [R,L]
And no, the ServerAlias command only works from a VirtualHost configuration:
http://httpd.apache.org/docs/2.2/mod/core.html#serveralias

Resources