Forum Moderators: phranque
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
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.
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
?
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.
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
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
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
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
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]
Links to more information about regular-expressions patterns and mod_rewrite are available in our Forum Charter.
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!