Forum Moderators: phranque

Message Too Old, No Replies

Permanent redirects

All in one rather than individually

         

abbeyvet

12:35 pm on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I have a site that uses a consistent naming structure thoughout directories, for ease of interlinking via a CMS

This means for example there are files like this.

/dirA/#*$!.php
/dirA/yyy.php
/dirA/zzz.php

/dirB/xxx.php
/dirB/yyy.php
/dirB/zzz.php

/dirC/xxx.php
/dirC/yyy.php
/dirC/zzz.php

And so on.

I have been doing some reorganising and have altered the names of some of the files, thoughout the directories, so that

/dirA/xxx.php becomes /dirA/vvv.php
/dirB/xxx.php becomes /dirB/vvv.php
/dirC/xxx.php becomes /dirC/vvv.php

In other words the page name changes are the same accross a number of directories.

I know I could list all the pages and the ones they should redirect to, but I don't know how to say something like:

In any directory files that were xxx.php are now vvv.php and ones previously called yyy.php are now ttt.php

I imagined something like this:

RewriteEngine on
RewriteRule ^xxx.php$ vvv.php
RewriteRule ^yyy.php$ ttt.php

but that is not working. Anyone able to help?

Birdman

12:51 pm on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You're close, but there are a couple errors. First one is not escaping the period(.). It's a reserved regular expression char. Second, you should use the [L] flag to tell Apache not to go any further after a match. Also, on the right side (the new URL), you should start from root (ie. /dir/file.php).

RewriteEngine on
RewriteRule ^/#*$!\.php$ /vvv.php
RewriteRule ^/yyy\.php$ /ttt.php

Keep in mind that those rules will only work for files that are in the same dir as the .htaccess file. You'd have to put that same .htaccess file in all the dirs. If you have multiple dirs, then I'd use something like this:

RewriteEngine on
RewriteRule ^(.*)xxx\.php$ $1vvv.php
RewriteRule ^(.*)yyy\.php$ $1ttt.php

The parens are for back-referencing. So, any file named xxx.php or yyy.php, in any dir will be rewritten and the dir will be prepended.

Hope I made sense there :) Definately check the documentation out for more info.

[httpd.apache.org...]

Cheers

jdMorgan

3:25 pm on Jul 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



For use in .htaccess:

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)#*$!\.php$ /$1vvv.php [L]
RewriteRule ^(.*)yyy\.php$ /$1ttt.php [L]

I added the both leading the slash on the substitution URL and the [L] flags as mentioned above.

The main point is that you may need the Options directive at the top to enable mod_rewrite.

Jim

gijimbo

6:39 pm on Jul 16, 2005 (gmt 0)

10+ Year Member



Hello JDMorgan,

What would it look like if you had hypens or underscores in the page name? Would it look like this?

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^(.*)oldpagename\.php$ /$1old-page-name.php [L]
RewriteRule ^(.*)olderpagename\.php$ /$1older_page_name.php [L]

I use a lot of underscores and hyphens.

Thanks,
Jim

jdMorgan

3:08 pm on Jul 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In general, that should work. If mod_rewrite has a failing, it's that it is so flexible that it's hard to grasp initially. Test your code and find out!

Jim

gijimbo

10:21 pm on Jul 18, 2005 (gmt 0)

10+ Year Member



Hello JD,

Is the above example suppose to automatically redirect people to the respective pages? If so, I am at a loss here. I have followed the example to the letter and my error log still shows "file not found" errors for my old page names.

I have been able to redirect the old pages to the new pages using an actual redirect.

I don't know if this makes a difference or not, but I am trying to do this.

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^/olddirectory/oldpage\.htm$ /$1new-page-name.htm [L]

Do you have any suggestions? Thanks in advance for your help. :-)

Jim

jdMorgan

12:22 am on Jul 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If your code is in .htaccess in the directory above both /olddirectory and /new-page-name.html, then remove the leading slash from the RewriteRule pattern:

RewriteRule ^olddirectory/oldpage\.htm$ /new-page-name.htm [L]

You've changed your example since your first post, which leaves me at a loss to understand exactly what your are trying to do. Mod_rewrite being a precise art, the problem must be precisely defined before starting to code.

If you will define what directory you wish to put the .htaccess mod_rewrite code in, what URLs you wish to rewrite, where you want to rewrite them to, which parts of the old URLs are fixed and which are variable, and the relationship of the new destination directory to the directory in which the mod_rewrite code resides, that will make a good start to defining the problem. The coding is not difficult; The difficulty is in defining the problem precisely enough so that everyone looking at this thread can understand it.

Jim

gijimbo

3:13 am on Jul 19, 2005 (gmt 0)

10+ Year Member



Hello JD,

Thanks :-) The last piece of advice worked. I apologize for the confusion. I have been trying multiple variations of various posts through this topic and other topics within this forum.

The last method has worked wonderfully. Is there a book like .htaccess for dummies? I would be interested in such a book. I can see the beauty and power of the .htaccess file.

Once again thanks for your time.

Jim