Forum Moderators: phranque

Message Too Old, No Replies

mod_rewrite going up one directory

         

DeanC

3:04 am on Nov 21, 2005 (gmt 0)

10+ Year Member




RewriteEngine On

# Rewrite old threads to the new urls #
RewriteCond %{QUERY_STRING}!seourl=1$
RewriteCond %{QUERY_STRING} ^t=([0-9]+)$
RewriteCond %{REQUEST_URI}!^(t¦thread)([0-9]+)(((-p)([0-9]+))?)([A-Za-z0-9\-;]+)\.html$
RewriteRule ^showthread\.php$ seourls.php?t=%1 [R=301,L]

# Rewrite old forums to the new urls #
RewriteCond %{QUERY_STRING}!seourl=1$
RewriteCond %{QUERY_STRING} ^f=([0-9]+)$
RewriteCond %{REQUEST_URI}!^(f¦forum)([0-9]+)-([A-Za-z0-9\-;]+)\.html$
RewriteRule ^forumdisplay\.php$ seourls.php?f=%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME}!^(showthread¦forumdisplay)\.php$
RewriteRule ^(f¦forum)([0-9]+)-([A-Za-z0-9\-;]+)\.html$ forumdisplay.php?f=$2&seourl=1 [L]
RewriteRule ^(t¦thread)([0-9]+)(((-p)([0-9]+))?)([A-Za-z0-9\-;]+)\.html$

showthread.php?t=$2&page=$6&pp=10&seourl=1 [L]

When I go to www.example.com/dir/showthread.php?t=1 it redirects to /home/fullpath/to/all/the/files/domain.com/lala/seourls.php?t=1

So it's not taking any notice of the relative paths. When I put a RewriteBase / in the rule it redirects www.example.com/dir/showthread.php?t=1 to www.example.com/seourls.php?t=1

Any ideas where I'm going wrong :)?

jdMorgan

3:24 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do not mix internal rewrite syntax with external redirect syntax.

Internal rewrite:


RewriteRule ^foo\.html$ /bar.php [L]

External redirect:

RewriteRule ^foo.html$ http://www.example.com/bar.php [R=301,L]

Salient differences: R=30x flag and canonical URL used in redirect syntax.

Internal rewrite simply changes the internal server filepath from which the content should be served; No client interaction takes place. External redirect sends 301/302 response to client, giving new URL. Client then re-requests content from new URL (Browser address bar will show new URL, search engine spider will update its URL database if 301 is used, 302 currently produces mixed ("sub-optimal") results across search engines, so avoid it).

Examples are for use in .htaccess context.

Jim

DeanC

3:36 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Thanks for the reply. I'm struggling to understand what you are trying to say in your message though. If you could elaborate that'd be great please :)

Thanks,
- Dean

jdMorgan

3:40 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Among other things, I'm saying that this is wrong, because the canonical URL is missing from the substitution:

RewriteRule ^showthread\.php$ seourls.php?t=%1 [R=301,L]

RewriteRule ^showthread\.php$ [b]http://www.example.com/[/b]seourls.php?t=%1 [R=301,L]

will likely work better.

Jim

DeanC

3:42 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Ah, ok. That's what I thought but this .htaccess file is being shipped along with a free product I'm developing. Getting the user to change the .htaccess file is not the ideal solution.

Is there any way around this, perhaps, that you can think of :)?

jdMorgan

3:43 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I should add that RewriteBase is mostly useful for compensating for Alias directives in httpd.conf when/if you don't have access to that file and ccan't put your rewrites there.

Jim

jdMorgan

3:44 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Portable solution:

RewriteRule ^showthread\.php$ http://%{HTTP_HOST}/seourls.php?t=%1 [R=301,L]

Jim

DeanC

3:56 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Thanks thus far Jim. Unfortunately, the HTTP_HOST solution merely redirects me one directory up. So http://www.example.com/dir/showthread.php?t=1 will go to http://www.example.com/seourls.php?t=1

DeanC

4:14 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Just to elaborate, the only solution I have had so far that works is to use RewriteBase /currentdir. But this is not really the ideal solution unfortunately.

jdMorgan

4:17 am on Nov 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the code is located in '/dir', that's what I'd expect given the rule you posted. Modify the substitution URL by adding /dir after the %{HTTP_HOST} part, or move the code into the directory where you want it to act.

Personally, I never rely on 'relative paths' in mod_rewrite; It's too dependent on proper server config, UseCanonicalName, and DocumentRoot settings to work well across multiple commercially-hosted servers. So I always 'root' my substitution URLs, even for internal rewrites, i.e. precede them with a slash.

Jim

DeanC

4:31 am on Nov 21, 2005 (gmt 0)

10+ Year Member



Ah well it would seem using the full URL is the only solution.

Thanks for your help Jim, it's much appreciated. And I hope I haven't kept you up too late (it's 4.30am here so I think i'll be calling it a day).

Night!