Mod Rewrite - Changing Base URLS - apache

I got my mod-rewrite done correctly, however (obviously) the linked files on that page are wrong (cause they assume they're one directory inside). Can I rewrite the the base url via mod rewrite for when that rewritecond is met?
Thanks

You probably want to try mod_proxy instead of mod_rewrite, because it has a feature (ProxyPassReverse) which is designed for this particular use-case.

Related

rewrite for almost everything under /

I'd like to add groups to my site, and for that I would like to get rid of a directory, thus the homepage of each group would be like: http://example.org/my-awesome-group instead of http://example.org/group/my-not-so-awesome-group
using the rule RewriteRule ^group/([a-z0-9-]+)/ is quite easy to rewrite each requests accordingly, but without it I'm having some headaches, I'm not sure how to tackle this. do you have any experience about it?
for example there's no group /javascript /css those have to be treated as actual directories on the file system.
thanks!
You can make rewriting rules conditional on whether or not the request refers to a file/directory that actually exists on the filesystem. In addition to the mod_rewrite documentation, take a look at this question here on SO that has an example configuration.

Add on domain mod rewrite to create pretty url

I have a main website www.site.co.uk and one of my add on domains is addon.co.uk. Site has an htaccess as does addon. The folder of which from the root would be www.site.co.uk/addon.co.uk/.htaccess ..I think!
Anyway currently I can do redirects within addon htaccess file fine, but its a database driven site and im trying to create pretty urls for it, so:
http://www.addon.co.uk/addonsites/some.php?id=page
would become:
http://www.addon.co.uk/id/page/
The mod I have in the addon htaccess file is the following:
RewriteEngine On
RewriteRule ^id/([^/]*)/$ /addonsites/some.php?id=$1 [L]
But this has no effect.
Well, the mod_rewrite module will perform translations on requests to the server, so when anyone requests the resource located at http://www.addon.co.uk/id/page/ the server will know that http://www.addon.co.uk/addonsites/some.php?id=page is the place to go.
However, mod_rewrite does in no way modify your existing links. I.e., you should rewrite the HTML (or scripts generating the HTML) to match the "new" way of linking. E.g., if you have ... somewhere on your site, you must make sure it is changed to ....
tl;dr
mod_rewrite handles incoming requests; it does not modify your output (HTML).

Apache URL Redirect Alternatives

One of my clients (before I came along) decided to use htaccess redirects as their form of URL shortening/search engine friendly URLs. They have literally thousands of them.
The new version of the site now has friendly urls but they aren't equivalent to their redirects so they still need them.
My question to you all is: Is there another way than to populate this file with thousands of lines of "Redirects /folder1 /folder2"?
Thanks
If you cannot make simple rules to catch all of them as in the #chris henry solution you can use the RewriteMap utility of mod_rewrite. You'll be able to write these thousand rules in a text file, then make this text file an hash file, and mode_rewrite will try to match url in this file (if it's an hash file it's quite fast). After that mode_rewwrite can generate a redirect 301 with the [L,R=301] tag.
Yep, look at using the Apache config (httpd.conf or httpd-vhosts.conf) to set up site wide folder aliasing. Eg:
Alias /folder1 c:/www/folder2
Look at http://httpd.apache.org/docs/2.0/mod/core.html#directory for more info.
Depending on how different the URLs being redirected are, one solution might be to come up with an rewrite rule that covers all of them, and maintain the short / long URLs in your application, or even a database.

Creating rewrite rules for multiple urls in the same folder

I have been asked by our client to convert a site we created into SEO friendly url format. I've managed to crack a small way into this, but have hit a problem with having the same urls in the same folder.
I am trying to rewrite the following urls,
/review/index.php?cid=intercasino
/review/submit.php?cid=intercasino
/review/index.php?cid=intercasino&page=2#reviews
I would like to get them to,
/review/intercasino
/submit-review/intercasino
/review/intercasino/2#reviews
I've almost got it working using the following rule,
RewriteRule (submit-review)/(.*)$ review/submit.php?cid=$2 [L]
RewriteRule (^review)/(.*) review/index.php?cid=$2
The problem, you may already see, is that /submit-review rewrites to /review, which in turn gets rewritten to index.php, thus my review submission page is lost in place of my index page. I figured that putting [L] would prevent the second rule being called, but it seems that it rewrites both urls in two seperate passes. I've also tried [QSE], and [S=1]
I would rather not have to move my files into different folders to get the rewriting to work, as that just seems too much like bad practise. If anyone could give me some pointers on how to differentiate between these similar urls that would be great!
Thanks
(Ref: http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html)
What I would do, is make /submit-review/ post directly to itself (or a php file) then once submitted redirect from within the PHP file.
It can be hard to force htaccess to maintain post values whilst redirecting etc
My friend found a solution to this one.
RewriteRule review/submit.php - [L]
Will catch the first rewrite and then prevent the next one, worked a treat!

Redirecting a Directory to a Script on Apache

So I'm playing with a script that makes it super easy to mirror images off of the web. The script works great (based off of the old imgred.com source, if you've seen that) problem is, it looks a little clunky when using it.
Currently, in order to use the script, you go to a url like:
http://mydomain.com/mirror/imgred.php?Image=http://otherdomain.com/image.jpg
What I'd like to do is to be able to go to:
http://mydomain.com/mirror/http://otherdomain.com/image.jpg
and have it redirect to the former URL, preferably transparent to the user.
I'm reasonably certain that this can be done via .htaccess with a MOD_REWRITE of some kind, but I'm getting frustrated trying to get that to work.
After messing with this myself, I found out that apache collapses any double slash in the URL before the query part into a single slash, and passes the result to mod_rewrite. Maybe that was giving you problems?
This might work for you (.htaccess in the mirror directory):
RewriteEngine On
RewriteBase /mirror
RewriteRule ^http(s?):/(.*) imgred.php?Image=http$1://$2 [L]
Don't know if your script accepts https addresses as well, so I included that just to be sure