Forum Moderators: phranque

Message Too Old, No Replies

.htaccess redirect/rewrite with page number variable being passed

         

Brody82

9:59 am on Mar 5, 2007 (gmt 0)

10+ Year Member



Hi,

I'm trying to get an old set of URLs redirecting to a new set (I've migrated my site to a new CMS) and am having trouble. Specifically I want to redirect requests to "/page/number" to "/content/view/number/1" where number is the article number.

The contents of my .htaccess file are as follows:

RewriteEngine On
RewriteRule ^(content/¦component/) index.php

...and various redirects like this:

Redirect /rss [someurl.com...]

I've tried adding the following below the second line in the above code with no luck...

ReWriteRule ^page/([^/]+)$ /content/view/$1/1 [L]

Any ideas? Thanks in advance!

phranque

10:41 am on Mar 5, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld brody82!

no luck...

what happens?

Brody82

11:15 am on Mar 5, 2007 (gmt 0)

10+ Year Member



Hi,

Links for the new CMS work (./content/view/1727/1/) but links to the old CMS style return a 404:

Not Found
The requested URL /page/1489/ was not found on this server.

Brody82

11:16 am on Mar 5, 2007 (gmt 0)

10+ Year Member



To clarify, the two links above are different links, I should have given examples with matching article numbers to make it clear, sorry!

jdMorgan

4:06 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do you have any other working RewriteRules?

If not, you'll need to 'set up' and 'enable' mod_rewrite. Depending on how your server is configured, you may or may not need the first directive, but you'll always need the second:


Options +FollowSymLinks
RewriteEngine on
#
RewriteRule ^page/([^/]+)/?$ /content/view/$1/1 [L]

This code must be located in example.com/.htaccess in order to properly rewrite "example.com/page/123" etc. I added the optional-trailing-slash pattern "/?" to the end of your pattern, in case you get requests for "example.com/page/123/"

Jim

Brody82

4:23 pm on Mar 5, 2007 (gmt 0)

10+ Year Member



Unfortunately I'm getting the same results, I literally have just the following in my .htaccess file, every other line is #'d

RewriteEngine on
#
RewriteRule ^page/([^/]+)/?$ /content/view/$1/1 [L]
RewriteRule ^(content/¦component/) index.php

Unfortunately I'm still getting 404s on any urls in the following format:

[domain.com...]

Where 123 is the article number corresponding to the new URL in the following manner:

[domain.com...]

Thanks for all advice so far, any more ideas?

jdMorgan

5:14 pm on Mar 5, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Then you have tested with the Options directive or you do have other working rules, right?

If you intend that the output of the first rule be passed to the second (an inefficient implementation, BTW), then you cannot use the [L] flag on the first rule, because that means that if it matches, then it is to be treated as the [L]ast rule on this pass...

Jim

Brody82

9:33 am on Mar 6, 2007 (gmt 0)

10+ Year Member



My understanding of this is that the first rule and the second rule are totally unrelated? The first rule is trying to rewrite any .com/page/123 urls to .com/content/view/123/1, the second rule is used to create .com/content/view/123/1 pages from the query strings that the system produces. Please correct me if I'm not thinking about this correctly though!

I am not using the options directive as it knocks my site over when I use it and I have no more rules present, they are all commented out with #'s.

jdMorgan

2:09 pm on Mar 6, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's possible you're understanding this backwards, then.

The first rule takes a requested URL (the result of a visitor clicking on a link on one of your pages), and rewrites it to /content/view/<something>/1

The second rule takes anything that starts with "/content" and rewrites it to index.php.

Therefore, the output of the first rule can be rewritten by the second rule, as long as there is no [L] flag on the first rule.

In "internal rewrite mode," which is what you are using here, the pattern on the left matches a browser-requested URL, and if there is a match, then Apache serves the content from the substitution URL-path on the right. So, the pattern on the left matches a URL which may or may not be directly associated with a real existing file, and the path on the right points to an existing file or script on your server -- Requested URLs on the left, real files on the right.

For more information, see the documents cited in the Apache forum section of the WebmasterWorld library [webmasterworld.com].

Jim