Jump to content

.htaccess rewrite dont work


Lovac

Recommended Posts

Hello all,

 

i have website with dyn url for example

www.domain.com/index.php?str=home

www.domain.com/index.php?str=about

www.domain.com/index.php?str=gallery

 

i want to rewrite like this:

www.domain.com/home/

www.domain.com/about/

www.domain.com/gallery/

and this .htaccess should give that but its not working

 

now i created this .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?str=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?str=$1

 

i have htaccess for password protect in that folder, so i know .htaccess is working

site is in ftp folder like this: /public_html/projects/testsite

chmode: 755

 

im doing something wrong or bad htaccess rewrite?

please help

Link to comment
https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/
Share on other sites

Define not working. Not doing anything noticable, 404 server error, 500 server error, shows wrong page, something else? How are you testing it? Are you typing in (or clicking on an address) such as www.domain.com/index.php?str=home and expecting it to magically change?

 

Given a request for:

www.domain.com/home/

 

And the following .htaccess:

Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?str=$1

 

You should see the contents of * **:

www.domain.com/index.php?str=home

 

*If you receive a '500 Internal Error' then you probably don't have mod_rewrite enabled.

**If you receive a '404 Page Not Found' error, then you probably want a forward slash directly before index.php.

i have menu that generates links like these

 

<?php


//check selected page is correct name format
if(preg_match('/^[\-_a-z0-9]+$/i', $page)){
   $file = "$page.html";
   //check page exists
   if(file_exists($file)){
      include($file);
      exit;
   }
}
//any faults display home page
include "home.html";


function PageLinks($page, $pages){
   $str = "";
   foreach($pages as $k => $v){
if($page == $k){
         $str .="<li  class=\"selected\"><a href=\"index.php?str=$k\">$v</a></li >\n";
      }else{
         $str .="<li  class=\"unselected\"><a href=\"index.php?str=$k\">$v</a></li >\n";
      }
         }
   return $str; 
}

?>

 

and navigation

 



<?php
//Full page list
$pages = array(
   home => "Home",
   services => "Services",
      );
//Get Page selected
$page = (empty($_GET['str']))?"home":$_GET['str'];

//Display links
echo PageLinks($page,$pages);

   ?>

 

i removed some links cuz they were in my native language

 

nothing happens when i click on menu bar, 2nd code generates it and change page, cuz its one index.php file and changes content, also changes

<body id="<?php echo $page ?>">

body id.

 

What should i do to make this to work?

nothing happens aka links stays the same, no 404 or 500 errors

In your OP you have two rules, one without a trailing slash and one with. You should note that in my first post I used Regex to make the forward slash optional (that's what the question mark means in that situation). If you use that rule then either should work.

That is because with the trailing slash you are essentially in a different 'directory', so relative URLs will not work, therefore if your links for things like CSS, JS and Images are something like 'images/header.jpg' or 'css/style.css' then the browser will look in the wrong location as it will be trying to look in '/about/images/header.jpg' instead of '/images/header.jpg'. The solution is to make all links to external files absolute paths or make them relative to root (begin with a forward slash).

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.