Struggling with mod rewrite.

Discuss practical ways rearrange URLs using mod_rewrite.

Struggling with mod rewrite.

Postby 7007 » Fri Oct 17, 2008 10:16 am

Hello,
I've been struggling with this almost all day long:

I have:
Code: Select all
http://mydomain.com/index.php?lang=eng&page=home


Which need to be rewritten as:
Code: Select all
http://mydomain.com/eng/home


My "/.htaccess":
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^(.*)/\(.*)\.html index.php?lang=$1&page=$2


Any help are appreciated!
Thanks,

Sasha
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Postby richardk » Fri Oct 17, 2008 2:19 pm

\( should be ( only.
You have .html in your rule.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?lang=$1&page=$2 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby 7007 » Sat Oct 18, 2008 8:48 am

richardk wrote:\( should be ( only.
You have .html in your rule.

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?lang=$1&page=$2 [QSA,L]


It does not work somehow. I'll try to play with it a bit more.
Thank you very much.
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Postby richardk » Sat Oct 18, 2008 9:45 am

What happens when you go to /eng/home/?
Where are you putting the mod_rewrite?
Where is index.php? Is it in your document root?

Try this mod_rewrite test.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby 7007 » Sat Oct 18, 2008 10:24 am

Thanks for your patience:
richardk wrote:What happens when you go to /eng/home/?

Outputs an empty white page (with no source even).
richardk wrote:Where are you putting the mod_rewrite?

In root/.htaccess of the document root.
richardk wrote:Where is index.php? Is it in your document root?

In the root. But it is not the landing page - it is made to redirect to template which then reads from folders (not that it's matter in this case I think).
richardk wrote:Try this mod_rewrite test.

I host a few other sites in the same place and the "mod_rewrite" is working fine, the given 'google' example works too.

Thanks,

Sasha
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Postby 7007 » Sat Oct 18, 2008 1:43 pm

The following worked for me:
Code: Select all
RewriteRule (.*)/([^/]+)/$ http://www.domain.com/index.php?lang=$1&page=$2 [QSA,L]


when I go to
Code: Select all
http://www.domain.com/eng/home/


Another question though: Is it possible to change the appearance of the URL in the browsers address bar? As it still appears as
Code: Select all
http://www.domain.com/index.php?lang=eng&page=home

or maybe I'm missing something?

Thanks,

Sasha
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Postby richardk » Sun Oct 19, 2008 10:58 am

Mod_rewrite does not change the links in your pages, you have to edit your script that outputs the URLs.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby 7007 » Mon Oct 20, 2008 3:38 am

richardk wrote:Mod_rewrite does not change the links in your pages, you have to edit your script that outputs the URLs.


I did - all the href's pointing as /$lang/$page/ structure.
I've tried the redirection as suggested here, but the result was "Redirect loop" or the page redirected to "http://www.http.com//mydomain.com/eng/home/".

Now the .htaccess is:
Code: Select all
Options +FollowSymLinks
RewriteEngine On
RewriteRule (.*)/([^/]+)/$ http://www.mydomain.com/index.php?lang=$1&page=$2 [QSA,L]


And the addition to index.php is:
Code: Select all
<?php

if(isset($_GET['lang']) && !empty($_GET['lang']))
{
  $url = 'http://mydomain.com/' . $_GET['lang'] . '/';

  if(isset($_GET['page']) && !empty($_GET['page']))
  {
    $url .= $_GET['page'] . '/';

    }
  header('Location: ' . $url, true, 301);
  exit();
}   
else
{
require_once('conf.php');
require_once('db.php');
require_once('func.php');
$page   = clean($_GET['page']);
$lang   = clean($_GET['lang']);

if(empty($lang))
{
   $lang = 'heb';
}
if(empty($page))
{
   header("location: http://"._site."/".$lang."/home/");
}

elseif(!file_exists($page.'/index.php'))
{
   attention('FILE <b style="color:#cc0000;">'.strtoupper($page).'</b> WAS NOT FOUND ON THE SERVER ... REDIRECTING TO HOME ...','images/error.png','#cc0000','');
}
else
{

   $tag = $db->query("SELECT * FROM `pages` WHERE `name` = '$page' AND `lang` = '$lang'");
   while($page_tag = mysql_fetch_array($tag))
   {
      require_once('template.php');
   }

}
}
?>
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Postby richardk » Mon Oct 20, 2008 11:49 am

Replace
Code: Select all
if(isset($_GET['lang']) && !empty($_GET['lang']))

with
Code: Select all
if(getenv('REDIRECT_STATUS') !== false && isset($_GET['lang']) && !empty($_GET['lang']))
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby 7007 » Mon Oct 20, 2008 4:03 pm

richardk wrote:Replace
Code: Select all
if(isset($_GET['lang']) && !empty($_GET['lang']))

with
Code: Select all
if(getenv('REDIRECT_STATUS') !== false && isset($_GET['lang']) && !empty($_GET['lang']))

No way, I tried - still ends up with an "Redirect Loop".
7007
 
Posts: 8
Joined: Fri Oct 17, 2008 10:05 am

Next

Return to Friendly URLs with Mod_Rewrite

Who is online

Users browsing this forum: Google [Bot] and 29 guests

cron