Forum Moderators: phranque

Message Too Old, No Replies

Change folder and meet file name condition

         

smallcompany

7:28 am on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If I renamed a folder and had to redirect all pages to the new one I would do this:

RewriteRule ^old-folder/(.*) http://www.example.com/new-folder/$1 [R=301,L]

Now I have multiple pages going into multiple subfolders. The clue is the string at the end of the page name. For example:

^old-folder/something-clue\.html going to the /old-folder/clue/same-file-name.html

If I have several pages like:

something-clue
something1-clue
something2-clue
...

How to I match that before "-clue"

I tried:

RewriteRule ^old-folder/(.*)clue\.html$ http://www.example.com/old-folder/clue/$1clue.html [R=301,L]

and got infinite loop.

I also tried:

RewriteRule ^old-folder/(.*)clue\.html$ http://www.example.com/old-folder/clue/$1 [R=301,L]

and got a redirect that worked to the point of "clue\.html$" which was missing.

What am I missing?

Thanks

[edited by: jdMorgan at 8:15 pm (utc) on Jan. 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

2:08 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you want
old-folder/<something-clue>\.html to redirect to /old-folder/<clue>/<something-clue>.html
or do you want
old-folder/<something-clue>\.html to redirect to /old-folder/<clue>/<something>.html ?

Can either <something> or <clue> (or both) have a hyphen in it? If so, what other characteristics can be used to find the boundary between <something> and <clue>?

Jim

smallcompany

6:36 pm on Jan 12, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks Jim.

I made initial mistake with "old-folder" idea. This is simpler:

folder/<something-clue>\.html to redirect to /folder/<clue>/<something-clue>.html

where "something" is the variable that is different from file to file while "clue" is identical for each group of files being transferred to the new subfolder.

jdMorgan

1:51 am on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you just moved the files into a few subfolders, then there's no need to change the URLs at all, just change the URL-to-filename mapping using an internal rewrite.

Serve content for the URL example.com/folder/<something-clue>.html from the file located at /folder/<clue>/<something-clue>.html :


RewriteRule ^folder/([^-]+-([^.]+))\.html$ /folder/$2/$1.html [L]

Pattern: "folder" followed by one or more characters not a hyphen, followed by a hyphen, followed by one or more characters not a period, followed by a period and "html". Note the nested parentheses used to make $1="something-clue" and $2="clue".

Jim

[edited by: jdMorgan at 1:52 am (utc) on Jan. 13, 2009]

smallcompany

7:26 am on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is good if I moved all files but I did not. It's only for some that carry that "clue" which corresponds to the new subfolder. That is why I still believe that the "clue" has to be hard coded. The help is one RewriteRule per clue instead of for each single page as I have various number of pages for each "clue".

I tried this:

RewriteRule ^folder/([^.]+)-clue\.html$ /folder/clue/$1-clue.html [L]

and similar, but keep getting into infinite loops.

What is wrong with ([^.]+) or $1 which should correspond to the grouping?

I'm just trying to pick the string which is everything but dot falling between "folder/" and "-clue.html"

That "just" seems to be quite a math for me. Sigh.

Thanks

smallcompany

9:03 am on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This did it!

RewriteRule ^folder/([\w-]*clue)\.html$ /folder/clue/$1.html [R=301,L]

The problem was a hyphen, actually not the hyphen but my ignorance about it.

I had multiple hyphens in page names which caused my initial formula(s) to fail.

Thanks

jdMorgan

4:10 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I did ask about those hyphens in my first post...

The "([^-]+-clue)" subpattern I posted should work equally-well, with less dependence on what regex library you have in the server OS. This may be of some concern if you ever change hosts, as not all support the "\w"-style notation.

Jim

smallcompany

7:43 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks.

I wanted to get "([^-]+-clue)" working but could not. I noticed that \w style is not used around much.

The problem was that [^-] means all but hyphen and I needed hyphen included as well.

I tried ([^.]-clue) which did not work. Then I tried ([^.]+-clue) which would create that loop.

What should I use in place of [\w-]*

Thanks

g1smd

7:53 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Something like
(([^-]+-)+clue)
instead?

smallcompany

8:05 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Oh, so hyphen must be explicitly added outside of the square brackets.

(([^-]+-)+clue)

([^-]+-) means all but hyphen plus hyphen, right?

...and than we just add the famous "clue"?

This should help in using $1 and $2 for folder as per Jim's original suggestion while keeping "clue" hard coded in the first part as a condition since not all pages are moving.

Thanks!

g1smd

9:25 pm on Jan 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Nearly.

(([^-][b]+[/b]-)[b]+[/b]clue)

is

"(([not a hyphen] one or more times, followed by hyphen) one or more times, followed by clue)"

So it allows multiple words with a hyphen between each one.

smallcompany

7:36 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Well, this produces bunch of "clue" folders or a "redirect loop" per Firefox.

Here is the full line:

RewriteRule ^folder/(([^-]+-)+clue)\.html$ http://www.example.com/folder/clue/$1.html [R=301,L]

The one that works is:

RewriteRule ^folder/([\w-]*clue)\.html$ http://www.example.com/folder/clue/$1.html [R=301,L]

?

Thanks

[edited by: jdMorgan at 8:16 pm (utc) on Jan. 15, 2009]
[edit reason] example.com [/edit]

jdMorgan

8:13 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yeah, sorry for that and the other previous typo as well. You'd need:

RewriteRule ^folder/(([^\-/]+-)+clue)\.html$ http://www.example.com/folder/clue/$1.html [R=301,L]

Note that the only advantage of this over your "[\w-]" pattern is portability across servers; We've had some reports here of the "token"-type regex (such as "\w" or "\d") not working on certain servers.

This is because the regular-expressions library used by mod_rewrite comes with the operating system of the server. It isn't part of mod_rewrite, and it isn't part of Apache. So if Apache is running on a Windows OS, then Windows provides the regular-expressions "processor", and if Apache is running on Linux, then the regex functions are provided by the library packaged with Linux. Because of this, support for various regex features can vary independently of Apache itself.

Anyway, feel free to use "[\w-]" if you're sure that you're going to be on the same same server for a long time, or if you feel prepared to modify the code in the future if your hosting set-up changes. I only suggested the modified pattern as being a somewhat more "robust" implementation over time and across hosting platforms.

Jim

[edited by: jdMorgan at 8:14 pm (utc) on Jan. 15, 2009]

smallcompany

8:24 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Thanks very much. This worked.

Now, the "\" before "-" was an escape, right?

But, what was that "/" before closing square bracket?

Thanks.

g1smd

8:30 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I guess that
[^\-/]+
is match until next hyphen or slash.

jdMorgan

8:32 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It should stop the loop you describe by prohibiting a match if a "/" is found. This should prevent the rewritten URL-path from matching this rule's pattern and being rewritten again and again.

Jim

g1smd

9:21 pm on Jan 15, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That's the 'clue' I needed.

I saw what it did, wasn't sure why it was needed. :-)