Apache mod_rewrite .htaccess and conditions to avoid rewriting while url mysite.com - apache

I'd like to make custom URL for my users so they can have their personnal art gallery displayed just like on facebook when it displays user's profile . For instance mysite.com/username will display username's gallery.
So basicaly what I've done now is something like that :
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
I have the gallery correctly displayed on my website, but I can't display the index page anymore using absolute mysite.com. I know we can use conditions to avoid such a situation but which one ?

So while writing the post I've just think about the line I've done and I found the solution ^^ !
Instead of using :
RewriteRule ^([a-zA-Z0-9]*)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
I'm now using :
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?fc=module&module=testmodule&controller=displaygallery&url=$1 [L]
Instead of using * I used +, which is telling Apache that it needs at least one character or more to rewrite URL so everything works fine now :)

Related

hide target url when redirecting using htaccess

I have a problem:
I have several customers with websites and e-mail-adresses. They use my providers webmail-service which is simply Roundcube.
So instead of having them all to go to
http://server20.shittyserviceprovider.com/webmail
I want them to hit their own website URL followed by /mail like
http://www.mybeautifulwebsite.com/mail
Now I tried the following:
RewriteEngine On
RewriteRule ^mail/?$ http://server20.shittyserviceprovider.com/webmail [P]
which kind of works but it does not load the functions and css of the mailprovider.
Is there a way I can get this done?

Prevent users from accessing files using non apache-rewritten urls

May be a noob question but I'm just starting playing around with apache and have not found a precise answer yet.
I am setting up a web app using url-rewriting massively, to show nice urls like [mywebsite.com/product/x] instead of [mywebsite.com/app/controllers/product.php?id=x].
However, I can still access the required page by typing the url [mywebsite.com/app/controllers/product.php?id=x]. I'd like to make it not possible, ie. redirect people to an error page if they do so, and allow them to access this page with the "rewritten" syntax only.
What would be the easiest way to do that? And do you think it is a necessary measure to secure an app?
In your PHP file, examine the $_SERVER['REQUEST_URI'] and ensure it is being accessed the way you want it to be.
There is no reason why this should be a security issue.
RewriteCond %{REDIRECT_URL} ! ^/app/controllers/product.php$
RewriteRule ^app/controllers/product.php$ /product/x [R,L]
RewriteRule ^product/(.*)$ /app/controllers/product.php?id=$1 [L]
The first rule will redirect any request to /app/controllers/product.php with no REDIRECT_URL variable set to the clean url. The Rewrite (last rule) will set this variable when calling the real page and won't be redirected.

Apache RewriteMap and hiding the URL

I'm trying to implement persistent URLs under Apache and I'm having trouble getting the URL passed back from the RewriteMap to remain hidden. That is, if I have the PURL:
http://www.mysite.com/psearch?purl=12345
and the mapped value for it is:
http://www.mysite.com/search?name=test&type=test2
I want the PURL to be the URL displayed in the browser address bar. Unfortunately, it keeps displaying the site that the PURL maps to instead. My rule is the following:
RewriteCond %{REQUEST_URI} /psearch(/)*$
RewriteMap mapper prg:/scripts/rewritetest.pl
RewriteRule ^/(.*)$ ${mapper:$1} [L]
All the mapper does right now is return a URL for a test page on the system, since I'm trying to get the address hiding working. And I know I'm not grabbing the parameters right now, I'm just trying to get the test running using the psearch keywork, and will add the rest later if it's possible to hide the address.
Any help is appreciated, Thanks!
Turns out the problem was that I was returning the full URL, which forced a full redirect. Passing back just the REQUEST_URI portion made things work.
Forcing the headers to expire also helped, since things were getting cached that were obscuring when something was working properly.

URL Rewrite to remove parameter but send it

I currently use register.php?referal=gamestand on my website so that when the user visits my website through the referal website, I can track how many players have registered from that place.
referal=gamestand will automatically fill in an input textfield within the registration process using echo $_GET['code'];
The problem is... on Google I have loads of links from these referals and I'd like to 301 them to my register.php page to merge the SEO score into one and eventually remove these silly register.php?code=gamestand style links from google.
I've got this rewrite rule... which removes the ?code=gamestand bit, but it doesn't actually pass the parameter, probably because it's redirecting and sending nothing?
RewriteCond %{QUERY_STRING} ^(.*)code(.*)$
RewriteRule ^(.*)$ http://localhost/mywebsite/httpdocs/register.php? [R=301,L]
it will change register.php?code=gamestand into register.php but as I said, $_GET['code'] is now empty and I'm back to square one!
Appreciate any help if anyone knows anything... Thanks! Dom
You could store the referrer in a session variable, then redirect to register.php without the code. Your URL won't contain the code any more, but it's available via the session.
session_start();
if (isset($_GET['code'])) {
$_SESSION['code'] = $_GET['code'];
header('Location: register.php');
exit;
}
...
echo $_SESSION['code'];
Redirecting these Urls to the same Url probably won't merge your SEO score into one. Your current method is fine, don't mess with it.

How does URL rewriting work?

I am new to URL rewriting and I have an .htaccess file that looks like this:
RewriteEngine On
RewriteRule /*\.(css|js|gif|png|jpe?g)$ - [NC,L]
RewriteRule "^(.*)$" "www/index.php?_url=$1" [QSA,L]
Does this code just rewrite the code internally, or is it supposed to change to URL in the address bar? As of now it does not change the address bar, and I'm not really sure yet but I am thinking that I will probably want the option to do so for bookmarking purposes. So if there is a way could you please let me know or direct me to a pretty noob friendly guide on URL rewriting where I can figure it out on my own because I haven't been able to find one.
Thanks for the help!
As it stands, it will just do an internal rewrite. To redirect the user (thereby changing their address bar), add R to the flags (e.g. [NC,R,L] or [R,QSA,L])
URL rewriting is completely server-side (unless you do a redirect). The client (and thus their address bar) will not know what the server is doing with the URL.
Here's a good beginner tutorial that explains URL rewriting and goes through progressively more complex examples.