APACHE multiple url routing - apache

thanks for clicking my question! <3.
Basically, I've tried to create a basic routing in my apache httpd installation, it looks like this:
RewriteEngine on
RewriteRule social/user social/profile.php
But if I want to create another routing to make it (for example) "social/user/sarah_thecowboy" it will open "profile.php" anyway, even if there is a rule getting a parameter. Is there a way I can do something like that?

Related

Apache `host/~username/` opening `/host/directory/`

I have the following:
host/~username/
sends the browser the page /home/username/public_html/index.html. In another directory /home/username/site_root/ we have the root of a website starting with its own /home/username/site_root/index.html.
How to do this: I would like that host/~username/ sends the browser to /home/username/site_root/index.html, if possible keeping host/~username/ as the address in the address bar.
I have never done anything with Apache. Reading some answers it seems that I might be able to do this by adding a file /home/username/public_html/.htaccess with certain commands in it. I have tried imitating what they do with lines like
RewriteEngine on
Rewriterule <...>
but I haven't managed to produce any result.
I tried to avoid using the word redirect because I don't know if that is the name of what I want to do. I don't want it to load some page that then sends the browser to another location.
I saw another thing that is done modifying httpd.conf which changes the root directory of the Apache server. I don't have access to this file.
Have you tried something simpler like a symlink?

How can I forward requests to http://server:port to http://server:port/app

I'm adding a new service to one of the servers I am currently running, and I'm having troubles getting both applications to run nicely.
Currently I have an web application running on http://server.com:8080/ and I need to be able to forward any of those requests to http://server.com:8080/app (or something similar). I've tried to do some ProxyPass and ReverseProxyPass configurations but I do not understand apache2 well enough to understand what is going wrong.
Can someone provide me with information on how to accomplish this?
You may use a rewrite rule like the following:
RewriteEngine On
RewriteRule ^/(.*) ^/app/$1

Help understanding mod_rewrite

Let's say I have the following filesystem setup on my webserver:
/www/web/foo/widget.php
...
/www/app/mvc/controllers/WidgetController.php
I need to figure out how to use mod_rewrite to map page requests (and their respective GET/POST data) for widget.php to its controller WidgetController.php.
It looks like mod_rewrite is super-powerful and thus complex. Is there a quick and easy way for someone to explain to me how to accomplish this? What files do I have to change? Can someone show me a sample rule for this "widget" example?
Thanks!
Nothing is quick and easy.
Setup
First you must make sure that you have the package installed
To use mod_rewrite, you need to load the extension. Usually, this is done by inmporting the rewrite.so module in the apache2 global configuration (/etc/apache2/apache2.conf)
Usually all mod_rewrite instruction are written in the virtual host definition. (Say: /etc/apache2/site-available/000default)
Usage
First step
To enable rewrite for one site, you have to ask for it with :
RewriteEngine On
Then you can begin to write rules. The basic you need to write rules is describe by the following diagram :
(See also : How does url rewrite works?)
To help me understand how it works, always consider it from the server side (not client side).
You receive an URL from the client. This URL has a certain format that you had defined. (E.g. http://blog.com/article/myarticle-about-a-certain-topic). But apache can't understand this by himself, so we need to help him. We know that the controller is page.php and can look up article by name.
Getting information
So now we forge a regex to extract information from the URL. All regex are matched against what is following your domain name (here : article/myarticle-about-a-certain-topic without the first / -- It can be written though on recent version of rewrite)
Here we need the article's name: ^article/(.*)$ will do the job of matching URL against article/<something> and capturing <something> into $1. (For characters meaning, I advise you to look a tutorial on regex. Here ^ is beginning of the string, invisible position after the .com/, and $ the end of the URL)
So now we need to informe apache that this URL means http://myblog.com/page.php?article=myarticle-about-a-certain-topic
This is achieved by using a RewriteRule
RewriteRule ^article/(.*)$ page.php?article=$1
Restricting to conditions
To go a bit on advance topics, you may want to apply this rule only if the article name is fetch by GET method. To do this, you can include a RewriteCond like
RewriteCond %{REQUEST_METHOD} GET
It goes BEFORE a RewriteRule in the file but is tested AFTER it.
Flags
If you are making lot of redirection/rewrite, you will have to understand flags
The most used are [L] and [R]. A little explanation on those :
[R] ask for redirection, it can be tuned like [R=302] where 302 is a redirection status number of the HTTP protocol. This will force the client to make a new request with the rewritten URL. Therefore he will see the rewritten URL in his address bar.
[L] forces apache to stop treating rules. Be advise that it does mean that the current incoming URL will stop being modified, but the rewritten URL WILL go again through the process of rewriting. Keep this in mind if you want to avoid loops.
Conclusion
So you end up with the following block of instructions
RewriteEngine On
RewriteCond %{REQUEST_METHOD} GET
RewriteRule ^article/(.*)$ page.php?article=$1
See also
You can find additional resources here :
A basic tester http://martinmelin.se/rewrite-rule-tester/
Cheat sheet : http://www.ranzs.com/?p=43
Query_String examples : http://statichtml.com/2010/mod-rewrite-baseon-on-query-string.html
Tips : http://www.noupe.com/php/10-mod_rewrite-rules-you-should-know.html and http://www.ranzs.com/?p=35

Page anchor with mod_rewrite?

I'm trying to set up page anchors on a website that uses mod_rewrite (Apache2 running on Ubuntu Server 9.04).
My htaccess file looks like this:
RewriteEngine On
RewriteRule ^information.php/([A-Za-z0-9-]+)/?$ information.php?display=$1 [NC,NE]
If I was using regular URL's the query would look something like this: http://mydomain/information.php?display=faq#cost
I'm hoping to get something like this:
http://mydomain/information/faq/cost
Is this possible? My understanding is that modrewrite ignores page anchors, and that the browser deals with it? I'm guessing that I can somehow use mod_rewrite to include the anchor information with the request, but I haven't been able to find anything documenting this and have been trying unsuccessfully to write it myself for hours.
Thanks!
Actually, if you want the resulting URL to have an anchor, then yes, it's possible. Just don't forget that in Apache configs, # marks the start of a comment.
If what you want is like this - user enters http://example.com/page/anchor and gets redirected to http://example.com/?p=page#anchor - you would need to use 301 Redirect, or something like that, so it wouldn't be transparent to the user.
Conclusion: While it is possible to write such a redirect rule, it can't be done entirely server-side. So I think you could point /information/faq to /information.php?display=faq and then use URLs such as: http://example.com/information/faq#foo which are almost what you want, plus they don't mess up caching.
(Whoa, it's midnight already?)

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