...

View Full Version : Trying to rewrite some URLs, confused.



samcroft
05-17-2009, 02:48 AM
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4-$5
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4
RewriteRule ^(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3
RewriteRule ^(.*?)/(.*)$ index.php?page=$1-$2
RewriteRule ^(.*)$ index.php?page=$1

I'm trying to get some URLs to convert like follows

http://www.domain.com/index.php?page=word1-word2-word3 to convert to http://www.domain.com/word1/word2/word3/

With those current rules it just goes to what index.php outputs, rather than index.php?page=word1-word2-word3 . The same goes if I go up to word5, or down to word2.

Although,

http://www.domain.com/index.php?page=word1 WORKS, it will convert to http://www.domain.com/word1/ - it just won't do anything further on.

Does anyone know what I need to do to make this work?

Thanks for any help you can provide, my knowledge of this is very limited.

oesxyl
05-17-2009, 07:00 AM
from this:

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html

see the "Ruleset Processing":

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#InternalRuleset

and read about "flags":

http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html#rewriteflags

try to solve your problem by adding the L flag to each rule.

best regards

samcroft
05-17-2009, 01:56 PM
I've tried addin [L] to each rule as follows:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4-$5 [L]
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4 [L]
RewriteRule ^(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3 [L]
RewriteRule ^(.*?)/(.*)$ index.php?page=$1-$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

With no success :(.

oesxyl
05-17-2009, 02:00 PM
I've tried addin [L] to each rule as follows:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4-$5 [L]
RewriteRule ^(.*?)/(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3-$4 [L]
RewriteRule ^(.*?)/(.*?)/(.*)$ index.php?page=$1-$2-$3 [L]
RewriteRule ^(.*?)/(.*)$ index.php?page=$1-$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

With no success :(.
try this way:



Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(.*?)\/(.*)$ index.php?page=$1-$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

best regards

samcroft
05-17-2009, 02:24 PM
I've managed to get this working, seems like it was a problem within the index.php file rather than .htaccess in the end.

----

In my scripts for all the image locations it is doing it as the new directory, /images

so its trying to say the images are in /word1/word2/word3/images

rather than /images

This is probably since the file locations are like this:


<img src="images/top_shared.gif">

Is there any way to make sure it does treat it in the right directory, rather than me having to replace them all with http://www.domain.com/images/image.gif ?

---

Also, it no longer lets me browse directories which were there previously, which have contents in which I need to view, it just returns the index page :(.

oesxyl
05-17-2009, 02:43 PM
try to put on top before other rules:


RewriteRule ^images\/(.+)$ images/$1 [L]

after first match redirection will stop because of L.

best regards

samcroft
05-17-2009, 02:50 PM
I've tried that with no success:



Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images\/(.+)$ images/$1 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4z$5 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3 [L]
RewriteRule ^(.*?)\/(.*)$ index.php?page=$1z$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

It is still coming up with the same wrong file path :(

oesxyl
05-17-2009, 02:55 PM
I've tried that with no success:



Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images\/(.+)$ images/$1 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4z$5 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3 [L]
RewriteRule ^(.*?)\/(.*)$ index.php?page=$1z$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

It is still coming up with the same wrong file path :(
can you post a link? also, take it step by step, comment all lines in htaccess and starting from top do change on the line untill work, then go to next line.

something like this:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images/(.+)$ images/$1 [L]
#RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4z$5 [L]
#RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4 [L]
#RewriteRule ^(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3 [L]
#RewriteRule ^(.*?)\/(.*)$ index.php?page=$1z$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

remove slash, maybe this is the problem.

best regards

abduraooft
05-17-2009, 02:56 PM
This is probably since the file locations are like this:


<img src="images/top_shared.gif">

Is there any way to make sure it does treat it in the right directory, rather than me having to replace them all with http://www.domain.com/images/image.gif ?
Yes, make them like
<img src="/images/top_shared.gif">
(The same absolute path technique can be used with files like css and javascript, to avoid messing their paths with the results of mod_rewrite )

oesxyl
05-17-2009, 03:04 PM
Yes, make them like
<img src="/images/top_shared.gif">
(The same absolute path technique can be used with files like css and javascript, to avoid messing their paths with the results of mod_rewrite )
that means "to replace them all", :)

best regards

samcroft
06-11-2009, 08:36 PM
Hi everyone,

Thanks for all of your help so far. I had to leave this problem for a while due to having to do some other things.

The image fix worked great, another similar problem has cropped up though.

I've got some folders which I want to get to on the site.

One of the folders I want to get to is http://www.domain.com/blog/ which has a wordpress blog inside of it.

When I try and load that URL, it just pulls up the site index page.

I added the same sort of code which I did for images but it isn't working.

My .htaccess at current:


Options +FollowSymLinks
RewriteEngine On
RewriteRule ^images\/(.+)$ images/$1 [L]
RewriteRule ^blog\/(.+)$ blog/$1 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4z$5 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3z$4 [L]
RewriteRule ^(.*?)\/(.*?)\/(.*)$ index.php?page=$1z$2z$3 [L]
RewriteRule ^(.*?)\/(.*)$ index.php?page=$1z$2 [L]
RewriteRule ^(.*)$ index.php?page=$1 [L]

Any help you can provide would be great, Thanks.

schleppel
06-11-2009, 09:49 PM
Relative paths can also be fixed with the HTML <base> element.


^blog\/(.+)$
does not match /blog/. Try

Options +FollowSymLinks

RewriteEngine On

# Ignore /blog and /images.
RewriteRule ^(blog|images)(/.*)?$ - [L]

RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1z$2z$3z$4z$5 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1z$2z$3z$4 [L]
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /index.php?page=$1z$2z$3 [L]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?page=$1z$2 [L]
RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]

samcroft
06-14-2009, 04:52 PM
Thanks, that worked great.

Something which I've noticed about the new way it processes URLs is that even if there isn't a index.php?page=XXXXXXXXXXX, that it will still give the homepage.

Is there any way it can be that if there is NOT a relevant index.php?page that it shows an not found error? I'm just thinking that this will make search engines like Google not too happy with content which is the same.

Thanks,
Sam

schleppel
06-16-2009, 11:50 PM
Do you mean when someone accesses example.com/fake/page they see the homepage and not a 404 error? To fix that you will have to edit your PHP script as mod_rewrite does not know if a page exists or not. If/when you do, send a 404 header with the PHP header() function

header('Content-Type: text/html', true, 404);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum