.htaccess tutorial

htaccess Elite


Need RewriteRule help with folders and relative paths

Ask your mod_rewrite and Redirection questions here, and get answers!

Need RewriteRule help with folders and relative paths

New postby psychopsi » 26 Feb 2007 17:44

I want to use rewrites to get rid of my php query strings.

First, I used:
Code: Select all
RewriteRule ^tkf/(.*)/$ tkf/index.php?page=$1  [L]

My working directory here is tkf but I plan to change that. I soon noticed that my css, js, and images were not showing up.

I then added these two rules to make them show up:
Code: Select all
#other files
RewriteRule ^tkf/(.*)/(.*)(\.css)|(\.js)|(\.html)|(\.txt)|(\.ico)$ tkf/$2$3 [NC]
#images folder
RewriteRule ^tkf/(.*)/images/(.*)$ tkf/images/$2 [L]


But now I have problems with relative links! Every page has navigation with anchors with href="news/", href="contact/" etc. etc. Whenever your in a folder the links look like tkf/contact/news.

I know there must be a better way to be doing this. I've seen a little about using some variables (like REQUEST_URI) but nowhere have I found just a simple explanation on how to implement this.

I'm probably going about this wrong so I need to be pointed in the right direction.

Please help.
psychopsi
 
Posts: 3
Joined: 26 Feb 2007 17:13

Re: Need RewriteRule help with folders and relative paths

New postby produke » 26 Feb 2007 18:40

psychopsi wrote:I'm probably going about this wrong so I need to be pointed in the right direction.

Please help.


It should be pretty easy actually... I just need a couple examples (not the domain) of the url before and after the rewrite.. so like.


What are the Urls that you want people to see? And what are the urls that they will translate too.

domain.com/files/img/34.jpg --> domain.com/index.php?image=/34.jpg
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

New postby psychopsi » 27 Feb 2007 07:16

I've managed to get my css, js, and images to show up, it's now that links that trouble me.

I think that I'll be making some of the links absolute instead of relative. Like, "http://example.com/tkf/news/" instead of "news/". Because if I'm on the news page, all of the links start with "news/" and that is the problem.

e. g. http://example.com/tkf/news/contact/ instead of the desired http://example.com/tkf/contact/

If I were to use something like,
Code: Select all
RewriteRule ^tkf/(.*) tkf/index.php [QSA]

and I dunno, use php's $_SERVER['SCRIPT_NAME'] or something to determine the url, would I still have this problem?
psychopsi
 
Posts: 3
Joined: 26 Feb 2007 17:13

New postby produke » 28 Feb 2007 01:46

psychopsi wrote:Like, "http://example.com/tkf/news/" instead of "news/".
Because if I'm on the news page, all of the links start with "news/"

e. g. http://example.com/tkf/news/contact/ instead of the desired http://example.com/tkf/contact/


If you can easily edit your pages head section, than add this to fix your problem.
Code: Select all
<head>
<base href="http://x.com/tkf/">
</head>

Now on your http://x.com/tkf/news/ page a link to contact/ will goto http://x.com/tkf/contact/


Also change your htaccess code to this
Code: Select all
RewriteEngine On
RewriteBase /tkf
RewriteRule ^/([^/]+)/$ /tkf/index.php?page=$1  [L,NC]


This will let you use urls like
  • http://x.com/tkf/welcome/ or
  • http://x.com/tkf/images/ or
  • http://x.com/tkf/news/
Which will be served from /tkf/index.php?page=welcome

It will not work for these urls
  • http://x.com/tkf/welcome or
  • http://x.com/tkf/images/this.jpg or
  • http://x.com/tkf/welcome/index.html
  • http://x.com/images/anything


If this doesn't fix everything then let me know plus please provide more url examples of what you have that is not working and what you want.
User avatar
produke
 
Posts: 242
Joined: 25 Sep 2006 04:48

New postby psychopsi » 01 Mar 2007 07:48

I've figured it out.

Thanks produke.
psychopsi
 
Posts: 3
Joined: 26 Feb 2007 17:13


Return to Redirect or Rewrite Questions