Forum Moderators: phranque

Message Too Old, No Replies

yet another 301 redirect problem

301 redirect from old url to rewrited url

         

meetzah2

3:21 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



Hello,

I had a URL structure like this:
http://www.example.com/modules.php?name=News&file=article&sid=8774

and I rewrote it to this:
http://www.example.com/article8773.html

using this code in my .htaccess file:
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1

Now I need to 301 redirect all the old dynamic URLs to the new, static URLs. Can you please indicate me a solution?

Thank you.

jdMorgan

3:49 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



What have you tried so far?

Have you seen the other examples posted in the Library and in the threads here?

Jim

meetzah2

8:13 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



first, I tried to add this to the and of the line: [R=301,L] , but the redirect was backwards, from the static URL to the dinamic one :(

second step, I tryed this:

RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
RewriteRule ^modules\.php$ http://www.example.com/article%1.html? [R=301,L]

but I get an error from Firefox that is telling me the page I try to access is redirecting in an "infinite loop"...

g1smd

8:19 pm on Dec 26, 2008 (gmt 0)

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



You need to list the redirect first and the rewrite last.

You need [L] on the end of the rewrite.

Split the two things apart with a blank line and add a # comment before each part, explaining what each part does.

jdMorgan

9:04 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, other than the points g1smd notes, and the need to escape the period in the first Rule's pattern, your code appears to be quite correct.

Flush your browser cache completely before testing.

Jim

meetzah2

9:44 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



thank you for your replyes.
my code looks like this right now:

RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/
RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]

.. but still don't work.
if I try to access example.com/article9999.html, it redirects me to example.com/article.html

may it be a problem that I have this code before in .htaccess ?

# code to redirect /index.php to /
RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

g1smd

9:53 pm on Dec 26, 2008 (gmt 0)

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



Other than add the [L] you didn't actually do any of the things i asked you to do.

Those things will fix the problem.

Jim also asked that you escape the period. You missed doing that too.

jdMorgan

9:54 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have not made several of the changes recommended above. Please re-read the previous three posts.

Jim

g1smd

9:59 pm on Dec 26, 2008 (gmt 0)

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



You'll also need a non-www to www site-wide 301 redirect, as well as the two redirects you already listed.

List all three of those redirects (in order from most specific, to general) before your rewrite. Some of your redirects have two lines of code (a condition and a rule) that must not be split apart.

Your index redirect is better coded without .* and as per the examples linked from the sticky thread at the top of the forum.

Split out your four bits of code (three redirects and one rewrite) with a blank line between each one. Add a # comment before each of the four blocks.

[edited by: g1smd at 10:21 pm (utc) on Dec. 26, 2008]

meetzah2

10:03 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



I must say... this truly a place to learn :)

here is my code again, if I [haven't made you angry] :

#redirect line
RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]

#rewrite line
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]

#don't know exactly what is that line for
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/

now, all my addresses go to example.com/article.html (no article number in URL)

thank you again for your time

Later Edit:

RewriteCond %{THE_REQUEST} ^.*/index.php
RewriteRule ^(.*)index.php$ http://www.example.com/$1 [R=301,L]

RewriteRule ^modules\.php$ http://example.com/article%1.html? [R=301,L]
RewriteRule ^article([1-9][0-9]*).html modules.php?name=News&file=article&sid=$1 [L]
RewriteCond %{THE_REQUEST} ^[a-z](0,9)\ /modules\.php\?name=News&file=article&sid=([1-9][0-9]*)\ HTTP/

[edited by: jdMorgan at 10:29 pm (utc) on Dec. 26, 2008]
[edit reason] tidied-up [/edit]

g1smd

10:16 pm on Dec 26, 2008 (gmt 0)

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



You'll need this order:

- index redirect (two lines - condition and rule) - but use the example without .* from other forum examples.

- modules redirect (two lines - condition and rule) - not sure why you have split them up; they must be together.

- non-www to www redirect (two lines - condition and rule) - get this new code from other forum examples.

- rewrite (one line)

Check all the previous comments for other stuff still missing.

Add four comments (one to each block) explaining what each block does.

[edited by: g1smd at 10:32 pm (utc) on Dec. 26, 2008]

jdMorgan

10:28 pm on Dec 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I think you would do very well to read the documentation cited in our Apache Forum Charter [webmasterworld.com], and look at the threads in our Apache Forum Library [webmasterworld.com].

It is quite impossible to get your code to work by "guessing" and it is quite dangerous to try -- This is server configuration code and you can easily ruin your search engine rankings with a single incorrect character. Please study the documentation to save yourself a great deal of trouble.

I would suggest replacing all of your code with this:


# Externally redirect only direct client requests for /index.php to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index.php\ HTTP/
RewriteRule ^(([^/]+/)*)index.php$ http://www.example.com/$1 [R=301,L]
#
# Externally redirect only direct client requests for dynamic URL-paths to static URLs
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /modules\.php\?name=News&file=article&sid=([0-9]+)\ HTTP/
RewriteRule ^modules\.php$ http://www.example.com/article%1.html? [R=301,L]
#
# Externally redirect to canonicalize the domain name if a non-canonical
# hostname is requested, in order to prevent duplicate-content problems
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
#
# Internally rewrite requests for static URL-paths to dynamic filepaths
RewriteRule ^article([0-9]+)\.html modules.php?name=News&file=article&sid=$1 [L]

Note that all of the hostname strings shown here as "www.example.com" (and as "www\.example\.com" when used in patterns) must be consistent -- Pick "example.com" or "www.example.com" as your canonical (preferred) domain name, and then be utterly consistent in using that domain and no other.

I have made several of the changes recommended above, as well as some other minor tweaks. All are intentional and either fix problems or improve performance.

Please do not use this code until you have read the documentation, analyzed this code with the documentation in-hand, and understand every character in every line. It is unsafe to do otherwise. It is also not a very good idea to have to depend on strangers posting in forums in order to be able to maintain your own Web site...

Jim

meetzah2

10:32 pm on Dec 26, 2008 (gmt 0)

10+ Year Member



thank you very much.
I promise I will not use this code before I understand it.

thank you again for your time.

mihai.