Forum Moderators: phranque

Message Too Old, No Replies

redirect about 100 old urls

         

BobMane

1:03 pm on Jul 5, 2011 (gmt 0)

10+ Year Member



i asked my host tech support to redirect about 100 old urls


like

domain.com/old-page1.html to domain.com permanent

domain.com/old-page2.html to domain.com/new-page.html permanent

etc...

they first put in a broad redirect so that anything

domain.com/sdfb,asfb
domain.com/waiuerylrhaw.khakha.kgn

would redirect to

domain.com/

[i think, but not sure] the broad redirect interfered with some of my code and caused some errors, so I asked them to use a different approach redirecting only my specified urls.


they told me to manually do it my self in cPAnel

so I did, one at a time entered all the redirect

now in my htaccess file all the rules are there



RewriteCond %{HTTP_HOST} ^doamain.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.doamain.com$
RewriteRule ^articles\/popup\/Gut5\.html$ "http\:\/\/domain\.com\/" [R=301,L]


but alot of them do not work



my host said it is because I have too many rules


any solutions of how to do this right?

g1smd

10:07 pm on Jul 5, 2011 (gmt 0)

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



Remove the two conditions. They are not needed.

Don't escape slashes in the regular expression patterns.

Don't escape anything at all in the target URL.

Don't mass redirect to the root.

BobMane

1:47 am on Jul 6, 2011 (gmt 0)

10+ Year Member



sorry this code was written by cPanel itself using its redirect tool

can you please write one correct line so i can use it as a template?


this is what cPanel writes if i tell it i want to send kagimedia.com.hustleharder/ (or any file in the folder) to kagimedia.com :

RewriteCond %{HTTP_HOST} ^kagimedia.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.kagimedia.com$
RewriteRule ^hustleharder\/index\.html$ "http\:\/\/kagimedia\.com\/" [R=301,L]

BobMane

1:57 am on Jul 6, 2011 (gmt 0)

10+ Year Member



for example


this



RewriteRule ^/hustleharder/(.*)$ /media-products/hustle-harder.html [L,R=301]

does not work to re-direct

[kagimedia.com...]
[kagimedia.com...]

to


[kagimedia.com...]

lucy24

2:26 am on Jul 6, 2011 (gmt 0)

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



Rule #1: Use "example.com" to keep your examples from turning into active links. You don't want people to go to your site-- especially if it doesn't really exist-- you want them to see exactly what you wrote. So it becomes
RewriteRule ^/hustleharder/(.*)$ /media-products/hustle-harder.html [L,R=301]

does not work to re-direct

http://example.com/hustleharder/HUSTLE_HARDER_Tribute.html
http://example.com/hustleharder/

to

http://example.com/media-products/hustle-harder.html

Get rid of the (.*)$ If you search the last few threads in this forum you will find about eighty posts by g1smd explaining why. Or, in English: .* means "there may or may not be some stuff here". Since you're not doing anything with it, you don't need to capture it in the first place.

this is what cPanel writes if i tell it i want to send example.com.hustleharder/ (or any file in the folder) to example.com :

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^hustleharder\/index\.html$ "http\:\/\/example\.com\/" [R=301,L]

If that is a direct cut-and-paste, then cPanel is bonkers.
1: You never escape anything in the target. Otherwise it will try to send you to the literal url
http\:\/\/example\.com\/
with all those slashes.
2: You do not need to escape slashes / at all, ever. (Unless you are working in JavaScript or related languages.) Some RegEx environments don't especially care if you escape things that don't need to be escaped, but Apache is picky.

Did you really mean .com. or did you mean .com/ ?

You don't need this:

RewriteCond %{HTTP_HOST} ^example.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example.com$

An adjoining thread in this forum explains in excruciating detail how to resolve everything to either example.com or www.example.com. If you did need this condition, exactly as you wrote it, it could be expressed simply as

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$

I wish I could say you don't need the $ after dot com, but with the proliferation of new tlds you do need to make sure they're not asking for example.comp or example.communication or, or, or, et cetera. Not that they'd be ending up in your .htaccess, but you never know.

BobMane

4:07 am on Jul 6, 2011 (gmt 0)

10+ Year Member



Did you really mean .com. or did you mean .com/ ?


I don't know but the address in my browser says

http://example.com/


on my home page



about the code i pasted....yes what i pasted was a direct paste.


it is generated when i choose these settings in cpanel


[tinypic.com...]



i thank you for the tips but still not clear on the correct line of code needed

g1smd

10:30 pm on Jul 6, 2011 (gmt 0)

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



The code that Control Panel produces is the worst RegEx code you will ever encounter on the web.

RewriteRule ^/hustleharder/(.*)$ /media-products/hustle-harder.html [L,R=301] 


Remove the leading slash from the RegEx pattern (immediately after the ^).

Remove the (.*)$

Add http://www.example.com/ before the target URL.

Read many of the older posts in this forum. There are literally 50 000 examples of how to do almost anything that mod_rewrite is able to do.

BobMane

10:25 am on Jul 7, 2011 (gmt 0)

10+ Year Member



do you mean like this?:

RewriteRule ^hustleharder/ http://www.example.com/media-products/hustle-harder.html [L,R=301]


because that did not work


I have attached the entire htaccess file for this Magento website [kagimedia.com ]

g1smd

6:41 pm on Jul 7, 2011 (gmt 0)

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



It "did not work"... presumably because it is "broken" or you didn't clear your browser cache before retesting.

Use the Live HTTP Headers extension for Firefox to see the server transaction in detail. That will be your biggest clue.

[edit]1. There's a rogue " / " immediately before http in your code.
2. The redirect must be moved so that it is listed before any internal rewrites. It is too late to redirect a previously rewritten request.

lucy24

8:34 pm on Jul 7, 2011 (gmt 0)

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



RewriteRule ^hustleharder/ http://www.example.com/media-products/hustle-harder.html [L,R=301]

This wording would take anything in the directory (trailing slash) "hustleharder" and send them all to the file "hustle-harder.html". Is that what you intended?

g1smd

8:35 pm on Jul 7, 2011 (gmt 0)

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



This wording would take anything in the directory (trailing slash) "hustleharder" and send them all to the file "hustle-harder.html". Is that what you intended?

That's what I grok from the OP.

This wording would take a request for any URL within the director (trailing slash) "hustleharder" and send them all to the single URL ending "hustle-harder.html".

BobMane

9:20 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



it is correct that i wanted everything in that folder to redirect to the new html page. (of course this is only one of about 100 redirects i need to make).

most of the redirects are just from one old html page to my new home page but a couple (in the name of efficiency) will redirect a whole folder.

not being an expert, i had no idea (and still dont) if adding it at the top would adversely effect something else in magento (magento being the unique beast that it is).

anyhow, i added this near the top

############################################
## this section is for redirecting legacy pages to new website

RewriteRule ^hustleharder/ http://www.example.com/media-products/hustle-harder.html [L,R=301]


and it redirects to the home page anything in the hustleharder/ that's better than nothing but still not as expected.

i attached the new htaccess file

[kagimedia.com...]

g1smd

9:33 pm on Jul 7, 2011 (gmt 0)

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



RewriteRules placed BEFORE
RewriteEngine on
will NOT be properly processed.

You're unlucky that it didn't send "Error 500 - Internal Server Error" as that would have given a quicker clue there is a problem with the code.


The overall game plan for mod_rewrite rule ordering is:
- block bad requests
- redirect non-canonical requests
- internally rewrite requests

BobMane

9:44 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



do you mean after line 113?

############################################
## enable rewrites

Options +FollowSymLinks
RewriteEngine on

i just pasted it there (on line 120) and still goes to home page. (but i can see it trys to first go to correct url)

lucy24

10:35 pm on Jul 7, 2011 (gmt 0)

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



still goes to home page. (but i can see it trys to first go to correct url)

Then it's meeting another redirect somewhere along the way. By this time you've fine-tooth-combed your .htaccess, ended the original rule with [L] and so on to eliminate the obvious suspects. Is it possible there are further .htaccess files in the /media-products or /hustle-harder directories? Old ones that you've forgotten all about?

BobMane

10:49 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



I am not sure Magneto is like a normal site with actual directories but I might be wrong. i do know there are much more than one htaccess file in magento but do not think that is the issue.

it might have something to do with this on line 150:


############################################
## rewrite everything else to index.php

RewriteRule .* index.php [L]

</IfModule>



I am trying to research this now.

g1smd

10:59 pm on Jul 7, 2011 (gmt 0)

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



The keyword there is "else".

If you use the [L] flag on the end of the redirect, the request is processed there.

BobMane

11:11 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



I am going to try with


############################################
## rewrite everything else to index.php

#RewriteRule .* index.php [L]

</IfModule>


but I am first trying to make sure magento doesn't need that on

g1smd

11:36 pm on Jul 7, 2011 (gmt 0)

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



That will stop the URL rewriting entirely. You do need that line.

You're missing a fundamental concept or two here. URLs are used out there on the web and files are used here inside the server. Mod_Rewrite deals with URL REQUESTS. Mod_rewrite looks at the literal "GET /someresource HTTP/1.1 request" sent by the browser and then either directs the browser to make a new request for a different URL (that's a redirect), OR it changes the internal pointer to indicate a non-default filename inside the server to get the content from (that's a rewrite).

It's too late to redirect to a new URL once you have started to access the server hard drive to actually pull the content. Attempting to do so will expose the previously rewritten internal path as a new URL.

BobMane

11:41 pm on Jul 7, 2011 (gmt 0)

10+ Year Member



thanks I see you are right on that.

as with all thing magento its turning out to be a bit more tricky


[magentocommerce.com...]

granted, the url_rewrites can be controlled by htaccess but there still might be more going on here. more research i guess.