URL Rewrite to remove parameter but send it - apache

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.

Related

Manipulate user's address bar with mod_rewrite

I have a page at example.com/themizer.php, but I want it to appear that it's actually located at example.com/themizer/ (or example.com/themizer/index.php) for all practical purposes. I know how to basically make an alias for it with mod_rewrite, but how do I make it appear that users are being redirected to that alias? Example: a user requests example.com/themizer.php and the address in their browser turns into example.com/themizer/ without actually redirecting. Is this possible?
With server-sided configuration, you can only accomplish this with a redirect. This does not necessarily need to be a problem. Just make sure that the urls on your site point to the fancy url and not to the internal url. Otherwise you generate a lot of requests that have to be redirected, instead of just redirecting the odd request that came in in an other way (e.g. through an external old url or old bookmark). You do it like this:
#External redirect
RewriteCond %{THE_REQUEST} ^GET\ /themizer\.php\ HTTP
RewriteRule ^themizer\.php$ /themizer/ [R,L]
#Internal rewrite
RewriteRule ^themizer/?$ themizer.php [L]
If you really must, you can use javascript to 'push' a new window state into the history, updating the address bar. This causes the "go to previous page" button in your browser to contain bogus though. In other words: Going to the previous page does not work as expected, which I would not recommend since there is a better option available. You can do it with the following javascript statement in browsers that support it:
window.history.pushState( null, document.title, "/themizer" );

Drupal - Redirect just the homepage

due to internal politics with a project I need to redirect only the homepage of a site built in drupal to a different url. I want to be able to access the rest of the site to show people but just that when it goes to the homepage it redirects you.
I want to create a copy of the homepage and then have the original homepage automatically redirect to the new url....meta refresh wouldnt work because there is lag time...
Anyone know how to do this with .htaccess ?
Thanks
Add new comment
You can try adding this rule as first rule:
RewriteRule ^$ /new-home-page [L,R]

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

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 :)

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.