Jump to content

Rewriting url with query string


Vape

Recommended Posts

I want to make it so when people go to /index.php?page=about.xhtml they are redirected to /about/

 

 

 

I understand that I can either do this using .htaccess or by using php.

 

 

 

I am replacing the old site with a wordpress installation, so it will have a new index.php file. I could integrate the php commands to get the page variable and then send headers into the new index.php, or as I said above, I could use .htaccess. From my limited udnerstanding of .htaccess it seems you have to do something special to get the variable out.

 

 

 

There are only 5 urls that I want to rewrite, so it doesn't need to use regular expressions or anything (I don't yet understand regular expressions either :))

 

 

 

If someone is linked to/has bookmarked /index.php, I want them to be redirected to / (that's easy enough)

 

If someone is linked to/has bookmarked /index.php?page=forms.xhtml, I want them to be redirected to /

 

If someone is linked to/has bookmarked /index.php?page=programme.xhtml, I want them to be redirected to /programme/

 

If someone is linked to/has bookmarked /index.php?page=about.xhtml, I want them to be redirected to /about/

 

If someone is linked to/has bookmarked /index.php?page=links.xhtml, I want them to be redirected to /links/

 

 

 

Old index.php file

 

<?php



include('header.xhtml'); 

if(!empty($_GET['page'])) { include($_GET['page']); } 

else { include('main.php');include('counter.php'); }

include('footer.xhtml');

?>

 

new Wordpress index.php file

 

<?php 

/* Short and sweet */

define('WP_USE_THEMES', true);

require('./wordpres/wp-blog-header.php');

?>

 

 

 

It would be kind of cool if I could integrate some kind of logging system to count the number of redirected hits so I can see that all this was only for the 3 people who actually bookmarked pages, but that's not really important.

 

 

 

Thanks very much!

Link to comment
Share on other sites

if mod_rewrite is enabled for Apache , you may be able to use the Rewriterule to achieve this

 

 

 

http://httpd.apache.org/docs/1.3/mod/mo ... ewriteRule

 

 

 

I'll use your third example

 

 

 

RewriteEngine On

RewriteRule ^/programme(.*) index.php?page=programme.xhtml [L,NC]



 

 

 

 

 

Or a little sample of how to achieve it via PHP function

 

 

 


function replace_for_mod_rewrite(&$s) 

{ 

$urlin = 

array( 

"'(?
"'index.php?page=something.xhtml'"

); 

$urlout = array( 

"/programme", 

"/something"

); 

$s = preg_replace($urlin, $urlout, $s); 

return $s; 

} 

}

 

 

 

Im horribly tired so please excuse any minor glitches in that code , its just a general idea of course anyways :P

 

 

 

as for the counter , an easy way could be to just create a column in the database (if using one) and do a

 

 

 

 $sql = "UPDATE tblName

SET columnName = columnName + 1";



*mediumint(8) as column type should work 

 

 

 

Again just a small quick example :P

I like to fart silently but deadly in movie theaters
Ard Choille says (11:41 PM):

I wouldn't dare tell you what to do m'dear

Link to comment
Share on other sites

I have rewritten the page's content - the page at /programme/ actually exists, I just want people to be redirected when they visit the old url. I can tell you now there's an extra } in that php, but I'll try and read/understand it anyway.

 

 

 

And yes mod_rewrite is enabled.

Link to comment
Share on other sites

Here's the code I ended up going with:

 

 

 

Options +FollowSymLinks



RewriteEngine On



RewriteCond %{HTTP_HOST} ^www\.mysite\.server\.com$ [NC]

RewriteRule ^(.*)$ http://mysite.server.com/$1 [R=301,L]



RewriteCond %{QUERY_STRING} ^page=(programme|about|links)\.xhtml$

RewriteRule ^index\.php$ http://mysite.server.com/%1/? [R=301,L]



RewriteCond %{QUERY_STRING} ^(|page=forms\.xhtml)$

RewriteRule ^index\.php$ http://mysite.server.com/? [R=301,L]



# BEGIN WordPress



RewriteEngine On

RewriteBase /wordpress/

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule . /wordpress/index.php [L]





# END WordPress

 

Thanks for the suggestions anyway Albosky :)

Link to comment
Share on other sites

You could also implement an HTML redirect using the meta tag:

 

 

 

 

 

 

The 10 is the number of seconds to display the page with the redirect before redirecting to blah.com, which should be a proper URL but isn't due to my laziness :D.

 

 

 

Of course this would have to be done manually, or with PHP:

 

 

 






Redirect your stupid self!

<?php

// Get GET variable

$reqpage = stripslashes($_GET['page']);

$arr_reqpage = explode(".", $reqpage);

$newpage = "//" . $arr_reqpage[0] . "//";

if(!isset($_GET['page'])) {

$newpage = "//";

}

if($reqpage == "forms.xhtml") {

$newpage = "//";

}



echo('');

?>



 

 

 

Please ignore any errors since it's 2:30 in the morning and I can't sleep.

sigon4.jpg

handed me TWO tissues to clear up. I was like "i'm going to need a few more paper towels than that luv"
Link to comment
Share on other sites

 

 

 

You could do that through the header() function and that's what I usually do anyway, but if you have a page that's displayed for 10 seconds that tells users to update their bookmarks and such then it provides information and doesn't "break" the back button, since you can just hit back then hit back within another ten seconds; not especially difficult if you ask me. I think that article was aimed at people who use the meta refresh and set it to a low time so that you have to hit back really really fast - which is unwarranted.

sigon4.jpg

handed me TWO tissues to clear up. I was like "i'm going to need a few more paper towels than that luv"
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site, you agree to our Terms of Use.