RewriteMap to choose an ID from the end of url and if it matches replace the url end with another url end - mod-rewrite

I thought I'd write here if someone could help me out here, I've been trying to get this thing to work for a few days but I can't really seem to understand how should I configure RewriteMap to get what I need.
RewriteEngine On
RewriteMap myredirects txt:/var/www/redirects.txt
RewriteCond %{QUERY_STRING} ^id\=([0-9]+)
RewriteRule ^/story/(.*)$ ${myredirects:%{REQUEST_URI}} [L,R=301]
Something like this I have configured but I am clueless how off I am from getting the configuration to work. As of now it doesn't work for sure.
What I would need is I have a webpage that is about to get its content deleted and I would need to redirect it to another page instead of a 404.
I have a site IE: book.com/story/[dynamic-url-from-page]?id=654672
I want to configure it so when the end of the url matches the one in RewriteMap text then it will Rewrite to book.com/firm?f=1233456
My redirects.txt is set up pretty much like this
/story/dynamicurlexample?id=59368 /firm?f=1154657
Is it possible to do what I need with this module?
Any help would be greatly appreciated, I'll try to progress on my own as much as I can but help would be great.
The main problem is that RewriteMap has the issue of recognizing "? question marks" before the "id=" therefore I can't simply Rewrite site to site, I need a workaround. It can redirect to a site with a questionmark but not from one.
Thanks for reading!
DeltX5

Related

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

how can I get my mod_rewrite to work?

I took a look around at other mod_rewrite questions and couldnt come up with an answer, so if anyone would point me to one that I may be duplicating I would appreciate it.
Anyways my question is: Why is my mod rewrite not working? I followed like 3 tutorials and spent a while reading forums and previous questions on this site, and couldnt get any of them to work. Ive tried doing it through my vhosts.conf file (I think its also known as the httpd.conf on some installations of linux) and through a .htaccess
Here is my .htaccess filed code
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^/article/([0-9]+)$ article.php?art_id=$1 [NC,L]
Here is the link im trying to get working: mysite.com/article?art_id=$1
To be honest I only have a slight clue as to how mod_rewrite works.
I have mod_rewrite installed (I don't manage the server, but the company I work for also has hosting services, thats how I have access to the vhosts.conf but I did not set the server up, and as far as I know the guy doing it is pro and set it up right)
I put this in to see if I could get it to work and it redirected me to my home page (I think that means its working)
# Options +FollowSymLinks
RewriteEngine on
RewriteRule ^fakepage\.html$ http://yoursite.com/ [R,L]
At least as far as your .htaccess file goes, using the rule you have will not work because the input to the RewriteRule will never have an initial forward slash on it. The rule should instead look more like this:
RewriteRule ^article/([0-9]+)$ article.php?art_id=$1 [NC,L]
However, what you have should have worked when you tried it in a virtual host (assuming you restarted the server), so I'm not sure if this is the actual issue. In what way is the rewrite not working? Are you getting 404 errors instead of being redirected to the file you were expecting?
Tim is correct about the slash at the beginning of your RewriteRule.
When trying to figure out Rewrites, it can be helpful to turn on logging:
RewriteLog "/var/www/mysite.com/rewrite.log"
RewriteLogLevel 9

Advice for Getting mod_rewrite to Work

I am trying to make clean URLs. I have written a simple rule on the .htaccess file and I have been told that it should work. However, it does not. I was wondering if it might have something to so with mod_rewrite not being enabled.
I am using Network Solutions shared hosting. I called the company and was told that mod_rewrite is enabled by default on the hosting I have.
Any advice on how I can check to see if mod_rewrite is enabled? Also, I would appreciate any related advice.
Thanks in advance,
John
EDIT: I just posted this at Serverfault.com, but I'm hesitant to delete it here since I have a feeling it might get more views here. Let me know if I should delete it here as well. Thanks.
EDIT:
Below is my .htaccess file. I am getting a 404 error when I go to a URL that this should re-write.
RewriteEngine On
RewriteRule ^comments/([A-Za-z0-9-]+)-([0-9]+)?$ index.php?submissionid=$2 [NC,L]
Try putting up a bad .htaccess file (i.e. random text) in a test folder. You should get all 500 errors from that folder. If not, .htaccess files are not enabled, and you'll have to get your hosting company to switch them on.
Then try a really simple rewrite rule:
RewriteEngine On
RewriteRule foo index.php
and see if that works. If you get a 500 error at this stage, mod_rewrite is probaby not enabled.

using mod_rewrite to redirect from subdomain to maindomain

My problem is that i have a functioning subdomain (sub.mydomain.com). sub is loaded in mydomain.com/sub.
What i would like to do is to redirect all requests to sub.mydomain.com to mydomain.com.
Somehow it seems that when im the subdomain i cannot access the rootfolder (main domain). I can get it working with from mydomain.com/sub to mydomain.com. But not from the subdomain.
Currently im using:
RewriteEngine on
RewriteRule ^(.*)/?$ /home/web/webuser/$1 [L]
When accessing sub.mydomain.com i get a 500 Internal Server Error.
Is there a restriction in accessing the main from a sub? (rights wise)
Or maybe another way of getting to main, perhaps something like (../$1)
Thanks
EDIT:
I only have access to .htaccess. So DocumentRoot cannot AFAIK be used in .htaccess file.
What about symlinks? I dont really know what it does, but i assume that it links two locations? The only code i found for that enables symlinks (Options +FollowSymlinks) - but this line doesnt say anything about what to link (perhaps im all wrong)
Btw. thanks for input so far !
I must admit that I did not fully understand your question. Do you want to redirect everything from sub.mydomain.com/whatever to mydomain.com/whatever? In that case, the following (put in the config file of your sub.mydomain.com) might work:
RewriteEngine On
RewriteRule ^/(.*)$ http://mydomain.com/$1 [R,L]
It redirects on the client side, meaning that the user will see mydomain.com/sub in the browser.
EDIT: I think I understand your question now. Yes, it's a permissions issue: If the DocumentRoot of your web site is /whatever/sub, then you cannot just access /whatever by adding "/.." to the URL. I hope you understand that this is a good thing. :-) mod_rewrite just changes the URL, so it cannot do that either.
So, to solve your problem, you need to either change the DocumentRoot of sub.mydomain.com or create a symlink that allows you to access the required directory (e.g. /whatever/sub/redir-target -> /whatever). Be careful with the symlink option, though, since it will create valid directories of infinite length on your file system (/whatever/sub/redir-target/sub/redir-target/...) and some scripts cannot cope with that.
EDIT2: Instead of a symlink, you might want to create an alias, e.g., something like this:
Alias /redir-target /home/web/webuser
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/redir-target/.*$
RewriteRule ^/(.*)$ /redir-target/$1
Still, I think the easiest solution is to change the DocumentRoot...
Why not try using a Redirect Directive from mod_alias?
It's difficult to provide a definitive answer without knowing more about your server configuration.
The following might work and is at the very least a decent starting point:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub\.mydomain\.com
RewriteRule (.*) /$1 [L]
Ideally that would go in your httpd.conf, but might work from a .htaccess file (again, more information about how your subdomains are setup would be helpful).

Resources