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
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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

You are miss-understand what mod_rewrite does. Type www.domain.com/home/ in your address bar, if you see the page loading then your mod_rewrite is working. You should change the links in your site to point at that address.

Link to comment
Share on other sites

thx alot, i understand now

www.domain.com/about works , when typing /about/ it doesnt work, doesent show site propely

now i need to figure how to rewrite php to generate links like i want to

thx again

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

ok i have made it

not sure why when using www.domain.com/about its all ok

and when using www.domain.com/about/ only text shows up without any image, on source when its /about/ shows everything identical to one when its loaded propely

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.