mod_rewrite to keep subdomain in URL - mod-rewrite

I have this application that I want to support multi languages.
I thought the easiest way would be to use sub domains aka
http://fr.domain.com/content
Now I created the sub domain on my server, pointing to the main root and indeed, the above URL is accessible.
The problem now are all my links, which are absolute.
Is there a way with mod_rewrite to catch the language from the URL and than rewrite the links to the same sub domain URL?
So if we are on http://fr.domain.com/content and click the link http://domain.com/link I want the page to load as http://fr.domain.com/link
Is that possible?
Cheers!

You would probably have to check HTTP_REFERER if you want to do this through apache. It might be good to start updating the site so that the links are dynamic in the future...
Something like (I can't really test this currently):
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_HOST} !^[a-z]{2}\.host\.com$ [NC]
RewriteCond %{HTTP_REFERER} ^http://([a-z]{2})\.host.com/.*$ [NC]
RewriteRule (.*) http://%1.host.com/$1 [R,L,QSA]
EDIT: removed a NOT in the 3rd condition
You might have to check some other conditions, but test things out to figure out what works. Plus, if you do other redirects you need a way to maintain the original referrer. In some ways even with the links the way they are, it may be easier to do this through a more dynamic means with php (through session) or something.
I was using these:
http://www.askapache.com/htaccess/mod_rewrite-variables-cheatsheet.html#HTTP_REFERER
http://www.askapache.com/htaccess/modrewrite-tips-tricks.html

You probably need some kind of on-the-fly HTML rewriting tool like mod_proxy_html. This tool was specically designed for rewriting the links in pages on the other side of a reverse proxy but it should be possible to use it for generic link rewriting. Specifically the docs say:
Normally, mod_proxy_html will refuse to run when not in a proxy or when the contents are not HTML. This can be overridden (at your own risk) by setting the environment variable PROXY_HTML_FORCE (e.g. with the SetEnv directive).
The module is quite configurable and supports conditional rewriting and regexes so with some tweaking it should do what you want.

So you're saying your website has links like <a href="http://domain.com/link"> instead of just <a href="/link">??? Is there some reason your links are coded like that?
Would you be happy with whipping out a text editor and search/replace'ing those hrefs instead of doing something a-typical or excessively complex with URL rewriting?

Related

Joomla trailing slash on categories

I would like to know how to add a trailing slash to joomla categories, but have no extension on articles.
Example, i want- xxx.com/category1/ and xxx.com/category1/article
Currently google sees- xxx.com/category1/ and xxx.com/category1 as duplicate content.
I would say you probably want to keep the URL without the trailing slash rather than the other way around. This just keeps it cleaner in the event that you have a category page which later needs a query string on the end of it. For example /categroy1?page=2 reads better than /categroy1/?page=2. This is personal preference though, Google does not really care so long as it's consistent.
The easy way to achieve what you are trying to do is through the .htaccess file in the root of your website.
Seems to be a few threads online about this subject. I've just tried a few of the solutions myself and the one that seems to work best is listed here https://forum.joomla.org/viewtopic.php?t=701030.
In summary you need to add the following to your .htaccess file just below the 'RewriteEngine On' statement.
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !administrator
RewriteCond %{REQUEST_URI} !component
RewriteRule ^(.*)/$ $1 [R=301,L]
Note that the Rewrite Base is needed, but if you are running your site in a sub folder (as you might in dev) you need to include that folder in the RewriteBase.
Apparently the !administrator and !component statements are needed to prevent some infinite loops when viewing the admin area, or a component content page without a SEF URL. There may be other exclusions that are needed.
Other options to solve your problem include canonical tags on the pages to tell the search engines which one you want indexed. Extensions to perform the redirect for you. Or if you have a really small site, use Joomla!'s redirect component to redirect every page you don't want to the one you do.
Hope this helps.
KevBallard

Rewrite numbers to default page

I am trying to rewrite a condition that can take any page that has numbers.html, lets say the like the following:
12345.html
33443.html
234545656.html
9797423.html
and just redirect it to index.php
RewriteRule ^([0-9]+)$ index.php [L]
I tried the following but it doesn't work, any help is very much appreciated.
Two immediate issues with your code:
you have to switch on the rewriting engine prior to using it and
if you want to match file names ending with .html, then you have to code that.
Have a try with this:
RewriteEngine on
RewriteRule ^([0-9]+)\.html$ index.php [L]
Then a few more hints:
the code you tried will only work in .htaccess style files. But the usage of such files has to be enabled first in your http server configuration.
a .htaccess style file has to be placed at the correct location, in this example within the folder actually holding the index.php file.
the rewriting module has to be installed and enabled, without those commands will not be available.
And a general remark:
Those .htaccess style files are notoriously error prone, hard to debug and they really slow down the http server, often for nothing. You should always prefer to place such commands inside the real host configuration of your http server. .htaccess style files are only offered as a last option for situations where you have no access to that configuration, for example when using a really cheap shared hosting provider.

Using mod_rewrite or htaccess to apply 301's to entire site structure, with URL's built using query strings

I am taking over an old website, and need to change the domain name.
The current domain is http://www.example.com/folder/ and the new domain is http://example.school.nz
An example of an existing and desired URL is:
Before: http://www.example.com/folder/index.php?page=sport
Desired: http://example.school.nz/about-us/sport
The site is built using CMSMS (http://www.cmsmadesimple.org), so pages and URL's are generated through the CMS, although it's easy enough to export a list of URL's.
Ideally I could set up 301 redirects for the entire site so the user doesn't get 404'd. Any clues to the easiest way to accomplish this?
It depends on how general you want things to be, but this set of rules will do what you asked in your question:
RewriteCond %{HTTP_HOST} .*
RewriteCond %{QUERY_STRING} page=sport
RewriteRule ^folder/index.php http://example.school.nz/about-us/sport? [L,R=301]

For SEO URLs via .htaccess do extensions need to be 301'd as well?

After researching, I have come to some conclusions for utilizing SEO URLs. For semi-static .php based sites that have file names such as index.php, about.php, contact.php etc I am using htaccess mod_rewrite rules so that for example 'www.mysite.com/about' can be used in place of 'www.mysite.com/about.php'. In such cases, all of my menus and links are then pointing at the SEO URL.
What I have found out though, is that even though nothing in my site references the .php extensions, if I enter them directly, they stick. So, I can get to the about page using /about or /about.php, and each will show as entered. It seems reading through posts here that this is a common issue.
My question is, does this matter? If nothing in my site references the .php extensions in a link or menu, do i need to be worried about duplicate content??
I was told I need to add something in htaccess like:
redirect 301 /about.php http://www.mysite.com/about
for each page to eliminate the possibility but I don't understand if this is really an issue in this case?
Two Questions:
Q1. "Does this matter"?
Answer: Not if you've never exposed the .php URLs before. If you have, then the answer changes to "yes".
Q2. What .htaccess?
Answer:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [R=301,L]
Your URLs are SEO friendly if there are some keywords in it that relate to the content and if they are not overly dynamic in a sense that Google Bot can interpret them (take a look at the Google Webmastertools and read this).
So I would not worry about "mysite.com/about" versus "mysite.com/about.php". I would rather watch out to avoid duplicate content if you want to change the URLs.
Here are some more links for further reference:
Making Website URLs SEO Friendly and Pretty
Some articles and advice on Duplicate Content

Rewriting a Subdomain with mod-rewrite

I have a project that uses the moodle library. I had to change the URL from moodle.example.com to learn.example.com, due to a client request.
I thought this would be an easy change, but alas moodle inserts all links and images in with the complete url instead of the relative url.
Is it possible using mod-rewrite to point all requests to moodle.example.com to learn.example.com and maintain the query string?
Example:
I want a request to: http://moodle.example.com/course/view.php?id=2&topic=1 to go to http://learn.example.com/course/view.php?id=2&topic=1.
Is this possible?
Thanks,
Josh
Try this rule:
RewriteCond %{HTTP_HOST} =moodle.example.com [NC]
RewriteRule ^ http://learn.example.com%{REQUEST_URI} [L,R=301]
In addition to the apache rewrite rules, it may also be worth looking at the moodle documentation on migration: http://docs.moodle.org/en/Moodle_migration
In particular look out for admin/replace.php. This tool can help you to rewrite links across all text in the moodle database at the same time.

Resources