Rewriting a Subdomain with mod-rewrite - 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.

Related

Rewrite URL's in Joomla menus

My group and I are working on a school project with Joomla. We have custom php to list "articles" from our database, and the PHP is getting its data from $_GET (from the url.)
Skip to the question:
This is what we have:
example.com/articles/menu
We want:
example.com/articles/menu?q=something+something
How do you rewrite / add text to url, so we get that format?
We are using the school server, so we have no admin-rights on the server.
I have tried mod_rewrite rules in .htaccess, but without success.
The server is running windows.
We are using joomla 2.5
RewriteEngine On
RewriteRule ^index.php/articles/menu?q=something+something$ /index.php/articles/menu [L]
Try this:
RewriteEngine On
RewriteRule ^/articles/menu$ index.php/articles/menu?q=something+something [QSA,L]
And that will do the job.
Hey by the way, try to google a bit, there are tons of answers for such simple things ;)
Checkout the right documentation about QSA directive: http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsa

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]

mod_rewrite to keep subdomain in URL

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?

How to solve TinyMce image manager Network Error

It was working until my hosting do some changes to server. I don't know what changes they have made.
request is: click here
or
http://mysite.com/tiny_mce/plugins/imagemanager/js/compressor.php?classes=mox.Event,mox.List,mox.DOM,mox.geom.Point,mox.geom.Rect,mox.String,mox.dom.Drag,mox.tpl.Template,mox.tpl.Paging,mox.data.ResultSet,mox.net.Cookie,mox.ui.WindowManager,mox.ui.Menu,mox.ui.MenuItem,mox.ui.DropMenu,mox.ui.DropMenuItem,mox.dom.Layer,mox.dom.Tween,mox.net.JSON,mox.dom.Form,mox.util.Dispatcher,moxiecode.manager.BaseManager,moxiecode.manager.ImageManager,moxiecode.manager.DevKit
it returns NetworkError: 404 Not Found
if i remove mox.net.Cookie and mox.net.JSON by hand from the querystring it works.
What should be the problem? How can i solve it.
I use CodeIgniter.
Thank you.
If you're using .htaccess it may be possible you're heading to a CI controller rather than the file you're looking for.
Here is an example mod_rewrite rule that might help. Often it's worth checking your access log to see which file is being called also.
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|media|favicon\.ico|ipadicon\.jpg|tiny_mce)
RewriteRule ^(.*)$ /index.php/$1

Disabling non-secure access for only one URL on one instance of apache

For example, one instance of apache is managing
www.site.com/folder1/a.html
www.site.com/folder2/b.html
www.site.com/folder3/c.html
I need to make sure that access to
www.site.com/folder3/c.html
is https only.
All these folders are in the same document root.
Is this possible? If not, what you recommend as the minimum changes necessary in order to get what I want?
I believe you can do something in the Apache configuration like:
RedirectMatch /folder3/(.*) https://www.site.com/folder3/$1
Not sure if that's proper though.
Hope that helps...
You can try this solution:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/folder3/c.html$ https://www.site.com/folder3/c.html$1 [R,L]
With this solution you can also define which kind of redirection to apply. And, the most important, the rule will only run when in HTTP scheme.

Resources