Jump to content

learning mod rewrite


purencool

Recommended Posts

hi

I am have been trying to get the mod rewrite to work on my site. I have use other examples  from around the web and i know that I can rewrite my url. But I am having trouble with getting mine right.

 

Below is my current url

http://www.purencool.com/index.php?mdir=5~News&page=1~News.php

 

I want it to look like this

http://www.purencool.com/News/News.php

 

This is what I have currently with my thoughts. If I have the wrong idea please tell me=)

This turns the engine on

Options +FollowSymlinks
RewriteEngine on

This starts from the root directory

RewriteBase /

This regex the get vars


RewriteCond %{QUERY_STRING} page=([a-zA-Z]+)
RewriteCond %{QUERY_STRING} mdir=([a-zA-Z]+)

And this is a bad attempt at a rewrite rule. I get lost at this point!

RewriteRule index.php index.php?mdir = %1 page=%1  [L]
[code]

Link to comment
Share on other sites

Hello again

Ok I done some research  and I know that this is close but i still can't get it to work.

The get variable will not rewrite. Can anyone see what I am doing wrong?

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
rewriterule ^([a-z0-9~]+)/([a-z0-9~.]+)/?$ index.php?mdir=$1&page=$2 [NC,NE,L]

Link to comment
Share on other sites

thanks for the reply

When some one comes to my site right now they see this

index.php?mdir=News&page=News.php

I want them to see this in the url

http://www.purencool.com/News/News.php 

 

and from what I can work out to achieve this you use mod rewrite

by placing this code in the root directory of the website in  a .htaccess

file. But I can't get it to work. But I have tested the mod rewrite in the

sub folders and it works fine

 

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
rewriterule ^([a-z]+)/([a-z.]+)/?$ index.php?mdir=$1&page=$2 [NC,NE,L]

Link to comment
Share on other sites

I am well aware of both what mod_rewrite is and what you are trying to do with it. So back to my previous question. Are you saying that (obviously when you have the rewrite rule in your .htaccess and the .htaccess file in the root folder) if you type in the desired URL you don't see the page you expect? Saying it doesn't work is not exactly helpfull. Obviously it doesn't work else you wouldn't have wrote a thread in the forum in the first place. What exactly is the problem? Does it not do anything? Does it show the wrong page? Do you get a 404 page not found error? Is that the only information you have in your .htaccess file? Does it break other links in your page from working?

Link to comment
Share on other sites

Sorry I miss understood,

 

It does nothing it will not rewrite the url

This is what my .htaccess file looked like yesterday in my root folder of my web site.

 

ErrorDocument 404 /_fpages/404.php

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
rewriterule ^([a-z]+)/([a-z.]+)/?$ index.php?mdir=$1&page=$2 [NC,NE,L]

 

But if you go here on my site you can see mod_rewrite working fine

http://www.purencool.com/_tools/htaccess_test/rewrite.php

 

Link to comment
Share on other sites


There is nothing wrong with that rewrite code, it works perfectly. Create a new blank index.php page, place the following inside it...

 

<?php
echo '<pre>';
print_r($_GET);
echo '</pre>';
?>

 

Then place both that index.php file and the .htaccess in your root folder and type http://www.purencool.com/john/smith into your browser. You should see on screen something along the lines of...

 

Array
(
    [mdir] => john
    

 => smith
)

 

... thus proving you have successfully been redirected.

 

Link to comment
Share on other sites

I worked great.

 

I had to type get vars in the index page you wanted me to create

the array printed

Array
(
      url =>/john/smith
    mdir]=> john
    page => smith
)

 

 

So it is rewriting the url for a search engine.

But it is not showing it in the browser url bar when I look around the site

Is that correct?

Link to comment
Share on other sites

If that is the case this is my .htaccess file right now

ErrorDocument 404 /_fpages/404.php

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
rewriterule ^([a-z]+)/([a-z.]+)/?$ index.php?mdir=$1&page=$2 [NC,NE,L]

 

and is this a screen shot of my browser as of this post. So what my browser bar should be displaying

is http://www.purencool.com/index.php/Web_Sites/Web_Sites.php. But it is not! Is that right?

 

[attachment deleted by admin]

Link to comment
Share on other sites

No, you have the concept of mod_rewrite completely and utterly backwards (which it seems is a fairly common mistake). The whole objective is to send the first link to the second link, not vice versa. Essentially you are masking the actual URL with a more readable version of it. You will need to edit your entire site so that anywhere you output a link, instead of having links of the second format, they will have links of the first format.

Link to comment
Share on other sites

If you are attempting to have your links as search engine friendly, the links on your site need to be domain.com/pretty/link not domain.com/index.php?mdir=pretty&page=link. Thats the whole point of the process.

Link to comment
Share on other sites

yes I am looking for domain friendly links.

 

the get vars are variable  that point to a dir and a file. The side menu is a directory scan then a  include statement places the and main content into the index file. I was always under the impression that I could take those get vars and then use mod_rewrite to change them into a url that is more pleasing to the user and search engines. Have I got this complete wrong mod_rewrite does not have this capability?

 

 

and example of the structure is

index.php
       |
       |_    1~Home (dir)
                  |_   1~Home(file)
                  |
                  |_    2~About Me(file)

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.