Forum Moderators: phranque

Message Too Old, No Replies

Redirect more dynamic URLs to one static URL

         

ThomasVJ

7:44 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



Hey,

I've been lurking here for some time now trying to find a solution - so far with no luck (probably because my brain is getting to old to grasp these redirect functions).

My situation is that I am moving from an messy oscommerce shop with 2 languages to 2 different domains based on Magento. All old URLs for products will be replaced with new URLs.
The old osCommerce shop served som 5-10 different URLs pr product pr. language.
I figured out how to redirect the plain standard URL for a product using a standard Redirect 301 like this:

Redirect 301 /old_product_name-p-300.html [domainexample...]

The other URLs for the product can not be moved this way. A not working example:
Redirect 301 /old_product_name-p-300.html?language=en [domainexample...]

The -p-300 part (-p-productnumber) is the productnumber and is unique for the OLD URLs so what I am thinking is that there must be some clever way to get the -p-productnumber.html part of the old URL and use that for parsing to the new URL - something like:

Redirect 301 /WILDCARD-p-300.htmlWILDCARD [domainexample...]

I know that this example is not taking the language part into account, but doing that is probably to complicated for me to understand, since a product could have the following URLs:

/old_product_name-p-300.html?language=en
and
/old_product_name-p-300.html?category_id=5&language=en
plus 5-10 other ones in the same style.

So instead of feeding 404 to users, I would settle with redirecting ALL non-standard URLs for a product to my native language version (danish).

So would a solution like this one:

Redirect 301 /WILDCARD-p-300.htmlWILDCARD [domainexample...]

be possible (with mod_rewrite) and if so, could someone please outline a proper example of how it is done?

Thank you

/Thomas

g1smd

7:50 pm on Jul 2, 2009 (gmt 0)

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



You will need a RewriteRule with [R=301,L] flags for this, not a Redirect. Redirect cannot see Query Strings.

You'll need a RewriteCond to test the QUERY_STRING too. Do not mix RewriteRule and Redirect here. Use RewriteRule for all of the rules.

As this is one of the most requested topics here (almost every day it seems), there are a very large number of prior examples to browse.

ThomasVJ

7:55 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



That is the sort of standard answer in here I was hoping not to get, but thank you anyway!

Have a great day.

/Thomas

ThomasVJ

8:03 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



Sorry g1smd, didn't mean to get rude - hope you didn't get offended - I am just getting so f*cking frustrated with the fact that my brain wont grasp this advanced redirecting.

But what you are saying is that if I use RewriteRule in my .htaccess then I shouldn't be using Redirect at all?

/Thomas

ThomasVJ

10:06 pm on Jul 2, 2009 (gmt 0)

10+ Year Member



This is what I could figure out in here so far for redirecting:
/old_product_name-p-300.html?language=en
and
/old_product_name-p-300.html?category_id=5&language=en
to
[domainexample...]

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} ^p-300$
RewriteRule [domainexample...] [R=301,L]

It doesn't work perfect as it doesn't remove the ?language=en or ?category_id=5&language=en from the end of the new URL.

How do I make the new URL end exactly at html

?

ThomasVJ

12:41 am on Jul 3, 2009 (gmt 0)

10+ Year Member



What it is I need to test for with the RewriteCond?

The p-300, which is unique for the old URLs but not present in the new URLs?

or

the ?language=en part?

g1smd

2:29 am on Jul 3, 2009 (gmt 0)

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



The reason your Rule does npt work is that you have no pattern to match. The part in bold is missing:

RewriteRule [b]pattern[/b] target [flags]

Yes, never mix Redirect and RewriteRule in the same .htaccess file.

The RewriteCond will test QUERY_STRING looking for the stuff after the question mark.

There isn't enough volunteers here to write the code for you, but I will point out that so far this month at last 5 people have asked almost the same question and several of those threads do have fully commented code that will get you started.

ThomasVJ

2:35 am on Jul 3, 2009 (gmt 0)

10+ Year Member



Here I am testing for the language=en part:

RewriteCond %{QUERY_STRING} ^language=en$
RewriteRule ^p-300$ [domainexample...] [R=301,L]

But it still redirects to [domainexample...]

I don't get this and I wont - I am to old for learning new tricks like this and will only be using this redirecting for this one system change. Do you people offer a paid service so that I dont have "to do my homework" myself or should I look elsewhere?

/Thomas

g1smd

2:51 am on Jul 3, 2009 (gmt 0)

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



Flush your browser cache and try again. That rule should be working.

ThomasVJ

3:23 am on Jul 3, 2009 (gmt 0)

10+ Year Member



I have flushed all my browsers (IE 8, FF, Opera, Chrome) and even testet here: [webconfs.com...]

and it still continues to redirect to [domainexample...]

Maybe the Magento .htaccess file has some flaws?

Here it is (including this redirect masked according to forum policies):

############################################
## uncomment these lines for CGI mode
## make sure to specify the correct cgi php binary file name
## it might be /cgi-bin/php-cgi

# Action php5-cgi /cgi-bin/php5-cgi
# AddHandler php5-cgi .php

############################################
## GoDaddy specific options

# Options -MultiViews

## you might also need to add this line to php.ini
## cgi.fix_pathinfo = 1
## if it still doesn't work, rename php.ini to php5.ini

############################################
## this line is specific for 1and1 hosting

#AddType x-mapp-php5 .php
#AddHandler x-mapp-php5 .php

############################################
## default index file

DirectoryIndex index.php

<IfModule mod_php5.c>

############################################
## adjust memory limit

# php_value memory_limit 64M
php_value memory_limit 128M
php_value max_execution_time 18000

############################################
## disable magic quotes for php request vars

php_flag magic_quotes_gpc off

############################################
## disable automatic session start
## before autoload was initialized

php_flag session.auto_start off

############################################
## enable resulting html compression

#php_flag zlib.output_compression on

###########################################
# disable user agent verification to not break multiple image upload

php_flag suhosin.session.cryptua off

###########################################
# turn off compatibility with PHP4 when dealing with objects

php_flag zend.ze1_compatibility_mode Off

</IfModule>

<IfModule mod_security.c>
###########################################
# disable POST processing to not break multiple image upload

SecFilterEngine Off
SecFilterScanPOST Off
</IfModule>

<IfModule mod_deflate.c>

############################################
## enable apache served files compression
## [developer.yahoo.com...]

# Insert filter on all content
###SetOutputFilter DEFLATE
# Insert filter on selected content types only
#AddOutputFilterByType DEFLATE text/html text/plain text/xml text/css text/javascript

# Netscape 4.x has some problems...
#BrowserMatch ^Mozilla/4 gzip-only-text/html

# Netscape 4.06-4.08 have some more problems
#BrowserMatch ^Mozilla/4\.0[678] no-gzip

# MSIE masquerades as Netscape, but it is fine
#BrowserMatch \bMSIE !no-gzip !gzip-only-text/html

# Don't compress images
#SetEnvIfNoCase Request_URI \.(?:gif¦jpe?g¦png)$ no-gzip dont-vary

# Make sure proxies don't deliver the wrong content
#Header append Vary User-Agent env=!dont-vary

</IfModule>

<IfModule mod_ssl.c>

############################################
## make HTTPS env vars available for CGI mode

SSLOptions StdEnvVars

</IfModule>

<IfModule mod_rewrite.c>

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

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{QUERY_STRING} ^language=en$
RewriteRule ^p-300$ [domainexample...] [R=301,L]

############################################
## you can put here your magento root folder
## path relative to web root

#RewriteBase /magento/

############################################
## workaround for HTTP authorization
## in CGI environment

RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

############################################
## always send 404 on missing files in these folders

RewriteCond %{REQUEST_URI} !^/(media¦skin¦js)/

############################################
## never rewrite for existing files, directories and links

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l

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

RewriteRule .* index.php [L]

</IfModule>

############################################
## Prevent character encoding issues from server overrides
## If you still have problems, use the second line instead

AddDefaultCharset Off
#AddDefaultCharset UTF-8

<IfModule mod_expires.c>

############################################
## Add default Expires header
## [developer.yahoo.com...]

ExpiresDefault "access plus 1 year"

</IfModule>

############################################
## By default allow all access
order allow,deny
allow from all

############################################
## If running in cluster environment, uncomment this
## [developer.yahoo.com...]

#FileETag none

ThomasVJ

5:24 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



My head is exploding now ...

This URL:
/old_product_name-p-300.html?language=en
redirects but still with the unwanted trailing ?language=en in the resulting URL
BUT
this URL
/something-else-p-300.html?language=en
gives a 404

I assumed that this rewriterule:
RewriteRule ^p-300$ [domainexample...] [R=301,L]
would redirect any request that has "p-300" in it and matches this rewritecond:
RewriteCond %{QUERY_STRING} ^language=en$

Apparently this assumtion is wrong (?).

/Thomas

g1smd

7:24 pm on Jul 3, 2009 (gmt 0)

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



Is your code in .htaccess or in httpd.conf?

There's a difference in whether a leading slash is needed on patterns in the RewriteRule.

ThomasVJ

8:08 pm on Jul 3, 2009 (gmt 0)

10+ Year Member



In .htaccess for the time being. The plan is to test that it works and then write it in the vhost.

jdMorgan

4:53 pm on Jul 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Your code is correct for use in .htaccess. Look for other causes:
Is *any* of the code in the .htaccess file getting executed for that request?
Is a script invoked when that URL is requested, and does that script re-append the language name/value pair?

One thing you can do is to intentionally 'break' the rule: Change it so that it redirects the request to google.com or something. That quickly shows if the rule is getting invoked and works or not. And if it does work, then you'll know that the problem is being introduced *after* the rule is invoked... Think outside the box... Divide and conquer. :)

Jim

jdMorgan

5:47 pm on Jul 5, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Re-reading this thread, I noticed the wildcard stuff in your first posts. I'm afraid that like many, I failed to pay sufficient attention to the first posts, and sort of "joined on in the middle" here.

If the requested URL-path does not *start* with "p-300", then you should not start-anchor your pattern. With a start-anchor, the requested URL-path must start with "p-300" in order to match.

Further, with an end-anchor, it will have to end right after "p-300" as well.

So you'll probably need to change the pattern:


RewriteRule ^([^-]+-)+p-300\.html http://www.example.com/new-product-name.html? [R=301,L]

Note that the start of that pattern matches against one or more non-blank "wildcard" strings which end with a hyphen, that "p-300" must be followed by a literal period and "html", and that the end-anchor has been removed, allowing anything to follow that .html string.

Links to more information about regular-expressions patterns and mod_rewrite are available in our Forum Charter.

Jim

ThomasVJ

9:40 pm on Jul 5, 2009 (gmt 0)

10+ Year Member



Thank you very much for the attention Jim.

I am getting slightly confused as redirecting is happening even when my .htaccess file has none of my custom redirects in it. This makes it very hard to do reliable testing - and yes, I have flushed the cache in all my browsers - I even send a test URL to a friend over Messenger and he had the redirect as well. Neither me or my friend is online through a proxy, so why this is happening is a mystery to me.
I have been looking through the backend of Magento to see if I could find any clues of Magento picking up redirect rules as they happen. In the database I can only find a trace of the requested URL being logged, but no redirection rules.

Porting from osCommerce to Magento is a way harder task than I first thought it would be. Once again I praise myself lucky that I only had some 1200 products to port and not 20.000 ...

Thanks again Jim and g1smd!