Liferay: Removing /web/ from all community sites - .htaccess

I can't seem to figure out how to do a url rewrite so that I can remove the /web/ from all of my community sites:
domain.com/web/communitysite1/page
domain.com/web/communitysite2/page
domain.com/web/communitysite3/page
domain.com/web/communitysite4/page
and I want it to be,
domain.com/communitysite1/page
domain.com/communitysite2/page
domain.com/communitysite3/page
domain.com/communitysite4/page
Here's a thread from Liferay, the solution there is using the virtual host; however, that's only for one of the community site for that one domain but I need the domain to be all the same for all the community sites I'm doing.

First, you will have to manualy change your pages links from domain.com/web/community... to domain.com/community
After that you need a rewrite rule to rewrite the new url to the old one without notifying users :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^web/
RewriteRule ^(.*)$ /web/$1 [L,QSA]
Then you will need a rule to redirect users that still access the old url to the new one while changing their browser link :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^web/
RewriteRule ^web/(.*)$ /$1 [L,QSA,R=301]
Then you need to mix that up, BUT, if you do, you will get an infinite loop, so you need something to avoid that (a trick I use is to add an optional parameter to the first rule and check in the second one if it exists or not, if it do then you don't redirect) :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} ^web/
RewriteCond %{QUERY_STRING} !(.*&)?r=0(&.*)?$
RewriteRule ^web/(.*)$ /$1 [L,QSA,R=301]
RewriteCond %{REQUEST_FILENAME} !^web/
RewriteRule ^(.*)$ /web/$1?r=0 [L,QSA]

Thank you #Yazmat for your kind help.
#Prakash K Here's what I am currently using, it's a different solution using proxy mod.
ProxyHTMLExtended On
ProxyHTMLEnable On
SetOutputFilter INFLATE;proxy-html;DEFLATE
ProxyHTMLURLMap ^/web/(.*)$ /$1 Rli

Related

ModX Can't Switch Contexts

I have a site that needs to be translated into three foreign languages. I created a new context through System -> Contexts, then created context settings for the base_url, site_url, and http_host. When I go to the home page in the new context, however, it routes to the File Not Found Resource from the initial context. Any ideas what I might be missing/doing wrong?
thanks
EDIT: Here are the uncommented lines in my .htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
(a lot of rewrite rules from an older version of the site that used .aspx files. None of them match with the URLs on the website)
# Add 'www' to the HTTP_HOST for all domains except seasidehotelshawaii.com
# this will also preserve the query string.
RewriteCond %{HTTP_HOST} !^(.*)[this_is_the_main_domain].com$
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
I don't know a lot about .htaccess files, but this is what is in there. this_is_the_main_domain.com is the parent of three child sites. I am trying to create three contexts for each of the child sites.
You have to make some pretty extensive changes to your htaccess file. I'm guessing you didn't write a context sweitching plugin either?
Follow these tutorials:
http://www.multilingual-modx.com/blog/2011/multilingual-websites-with-modx-and-babel.html
http://www.multilingual-modx.com/blog/2011/seo-friendly-multilingual-websites-with-modx-and-babel.html
Basically, you have not given modx a method of changing context or given it a way to generate your urls correctly.
Try using the XRouting or ContextRouter plugins to route your domains to the proper context.

Set up of conditional redirect in htaccess

I've been asked to make an existing web site multi-language.
In preparation for this I have had to move all existing pages from /path/page to /en/path/page
To maintain any existing incoming links I now need to set up an htaccess redirect to send any requests from their original urls to the new /en/path/page urls but I'm having trouble getting this to work.
This is what I currently have;
RewriteCond %{REQUEST_URI} !^/en$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
Which I think is meant to check the requested URI and if it doesn't begin with /en then prepend /en onto the requested URI... but I'm obviously mistaken since it doesn't work.
Any help appreciated. Thank you.
UPDATE.
Since this is an ExpressionEngine site and there is an additional rule to remove the index.php portion of the URL here are both rules
# Rewrite for new language based urls
# This is to try and get all current pages going to /en/(old url) with a 301 redirect
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png|ico)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
I have also tried this with the language rewrite after the index.php one. I'm still getting stuck in loops.
What it does is, checking whether the URI is not exactly /en, since the $ indicates the end of the string right after en.
Try this, it checks whether the URI is not exactly /en or /en/ or doesn't start with /en/, and if that's the case it will prepend /en/:
RewriteCond %{REQUEST_URI} !^/en(/.*)?$
RewriteRule ^(.*)$ /en/$1 [R=301,L]
update Considering the other rules you have in your .htaccess file, it is necessary to have the language rule not match again for the following internal redirect to /index.php..., otherwise you'll end up with an endless loop.
There may be better ways to prevent this, however the first thing that comes to my mind would be checking for index.php in the first condition:
RewriteCond %{REQUEST_URI} !^/(index\.php|en)(/.*)?$
So this will cause the rule not to apply after the internal redirect. But be careful, this solves the problem for this specific case only in which the internal redirect goes to index.php!

Create .htaccess with a variable

I am having trouble finding a solution for what I want to do. I am going to have a couple thounsand affiliates signing up on our site. They will provide a username, and basically give out the url www.domain.com/affiliate/username to their clients.
So, the url I will be starting with is www.domain.com/affiliate/home.html?userid=bob (if bob is the username they give).
I need that to check with the mysql database if that userid exists, then redirect it to the www.domain.com/affiliate/bob url.
Right now, I have this in my .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^\/]+)$ affiliate/home.html?affiliate=$1 [L]
It obviously doesnt work and Ive never dealt with .htaccess before. Any help is greatly appreciated. Thank you.
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^affiliate/([^/]+)/?$ /affiliate/home.html?affiliate=$1 [L]
As for redirecting to the nicer looking URL if someone enters the one with the query string:
RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /affiliate/home.html\?affiliate=([^\ &]+)&?([^\ ]*)
RewriteRule ^ /affiliate/%2?%3 [L,R=301]
There's no way to check if a username is in your database before the rewrite happens, using an htaccess file. There's ways to talk to a database in apache 2.4 or using a RewriteMap (which can only be defined in server or vhost config, not in htaccess) but you're better off doing the check in your home.html and just return a 404 if the user isn't found.
See my answer on an other similar question.
You want www.domain.com/affiliate/home.html?userid=bob to be redirected to /affliate/bob. You want /affliate/bob to be rewritten to /affiliate/home.html?userid=bob.
Add this to your .htaccess in your /www/ or /public_html/.
RewriteEngine on
RewriteCond %{QUERY_STRING} ^affiliate=([^&]*)$
RewriteRule ^affiliate/home\.html /affiliate/%1? [R,L]
RewriteRule ^affiliate/(.*)$ affiliate/home.html?affiliate=$1&r=0 [L]
The first rule matches if the user tries to access affiliate/home.html?userid=bob. The RewriteCondition matches the part of the query string (after the ?) that you need to write the fancy url. %1 matches the first 'capturing group' in a rewrite condition. The ? at the end will clear the query string. [R] means it is a redirect (see the linked answer what that does). [L] means it will stop matching if this one matches.
The second rule is an internal rewrite. $1 matches the first 'capturing group' in the RewriteRule. The trailing &r=0 is there to stop the first rule from matching. It requires a RewriteCond %{REQUEST_FILENAME} !-f to function properly, because the fancy url is in the same directory is the file that handles it.

htaccess, rewrite multiple pages to the same page but keep original value somewhere

I have been fiddle farting around with htaccess and RewriteEngine but I can't quite get my head around it...
I'm building a website on which I want users to be able to go to /portfolio/typography for example. But I don't want to create seperate pages for each category in this portfolio and thus I want to rewrite (redirect?) all the requests that go to /portfolio/ to the index.php of this directory and load the appropiate projects for this category from there.
Any ideas on how I could do this? I used this to redirect all the requests to /portfolio/:
RewriteCond %{REQUEST_URI} !/portfolio/$ [NC]
RewriteCond %{REQUEST_URI} !\.(jpe?g?|png|gif) [NC]
RewriteRule .* /portfolio/ [R=302,L]
Thanks in advance,
Cas Cornelissen
EDIT
Maybe I should note that I have another .htaccess file in the root of my website.
Ok, so I found the answer to my own question...
Seems like the following is working:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1?%{QUERY_STRING} [L]

shared ssl 404 w/ existing htaccess - follow-up to /questions/12737308/

I hope asking a new question is the right approach to follow-up to an existing question(?) - please bear with me if not, I'm pretty much new to stackoverflow. That said, I'm referring to hide html extension + redirect .html version + special case exception
The htaccess provided by user Jon Lin works perfectly fine so far, but now I'm struggling with a related problem (I think it's related to the htaccess) and that is to use a shared ssl certificate for just one specific page on our site, let's call it baz.html
Due to the existing/cited htaccess, hxxp://mydomain.com/baz.html resolves to hxxp://mydomain.com/baz, which is fine. But, trying to use a shared ssl at hxxps://shared.host.com/~user/baz(.html) leads to a 404 not found error. I'm pretty sure (I assume so) that this is due to the used htaccess as the shared ssl tutorial provided by our host is pretty much unmistakeable.
Also, the document root (foo.html) shows up (as the only not-404) when accessing it with shared ssl but the CSS(+images), although relatively linked within the html code, isn't processed. I assume(?) this is related.
Basically, what I'd like to achieve is linking hxxps://shared.host.com/~user/baz instead of hxxp://mydomain.com/baz within our main site navigation to achieve a secured page (same as I do now for foo(.html), bar(.html), just for unsecured content)
I hope that my actual problem is perfectly understandable(?). I do not want to link our actual project page to not do self-advertising of some sort. If access to the actual page is needed to answer the question I'll certainly provide a link (if explicitly asked for)
Thanks in advance.
You can't use the same htaccess file in your shared SSL host because the base is different (/~user/ instead of just /), and that will change how all the rules inherently work. So for those same rules, you need to account for the different base. If you put these rules in the htaccess file in the /~user/ directory, they'd need to look like this:
EDIT: since they're the same document root, you need to duplicate all the rules, one for SSL, one not:
RewriteEngine On
# 1. hiding the .html extensions
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/~user/%1.html -f
RewriteRule ^ /~user/%1.html [L]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1.html -f
RewriteRule ^ /%1.html [L]
# 2. 301 redirecting .html version
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~user/([^\ ]+)\.html
RewriteCond %1 !foo$
RewriteRule ^ /~user/%1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^\ ]+)\.html
RewriteCond %1 !foo$
RewriteRule ^ /%1 [L,R=301]
# 3. typing /foo.html to resolve to / as well as typing /foo to resolve to /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/~user/(.*?/?)foo(\.html)?$
RewriteRule ^ /~user/%1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/(.*?/?)foo(\.html)?$
RewriteRule ^ /%1 [L,R=301]
Now for the /baz and /baz.html to redirect to SSL, you'd need to add this before any of the rules (right under RewriteEngine On) in your domain.com document root (the non-SSL one):
EDIT: since the document root is the same, you need to add the condition to check whether it's SSL or not
RewriteCond %{HTTPS} off
RewriteRule ^/?baz(\.html)?/?$ http://shared.host.com/~user/baz [L,R=301]

Resources