.htaccess dynamic url (old site) to static url in magento - php

Long time reader first time poster.
I have read through what feels like 100's on similar posts trying to make it fit what i am trying to do.
Apologies if its been covered. I believe I have the correct code but it maybe conflicting with something else?
I have old URLs from an old ASP cart and want to 301 redirect to new magneto static pages.
There are only 5 of these URLS so don't need a bulk action.
example:
OLD URL: http://test.com.au/shopcontent.asp?type=FAQ%20and%20Terms
NEW URL: http://test.com.au/privacy-policy-cookie-restriction-mode
I know that from reading other posts you can not simply use:
Redirect 301 /shopcontent.asp?type=FAQ%20and%20Terms /privacy-policy-cookie-restriction-mode
So I have worked out that this should work:
RewriteCond %{QUERY_STRING} ^type=FAQ%20and%20Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
I have also tried removing the %20 for the spaces like this:
RewriteCond %{QUERY_STRING} ^type=FAQ\ and\ Terms$ [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]
When I try and access the old URL it just goes to the 404 page. I am using standard .htaccess file that came with Magento (I can post if that helps);
I am using a VPS which mod rewrite is enabled. I have other Redirect 301's for non dynamic URLS.
Are there any logs that I can look at?
Appreciate any help or advice.
Thanks Rudi
ps. I found this post which got me to where i am now
RewriteCond %{QUERY_STRING} in mod_rewrite (dynamic to static URL) not working

You cant use this rule:
RewriteEngine On
RewriteCond %{QUERY_STRING} "^type=FAQ\%20and\%20Terms$" [NC]
RewriteRule ^shopcontent\.asp$ /privacy-policy-cookie-restriction-mode/? [NC,R=301,L]

Related

htaccess redirect in wordpress

By using WordPress with Divi and custom php pages/scripts I ended with urls looking like this:
https://example.com/?day=today
https://example.com/?sport=1&league=123 (1=football and 123=premier_league)
I want to make them more user and SEO friendly and rewrite them in htaccess to:
https://example.com/?day=today ==> https://example.com/today
https://example.com/?sport=1&league=123 ==> https://example.com/football/premier_league
For the "day" link I tried this code, but it doesn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9a-z\-\_]+)?$ /?day=$1 [L,QSA,NC]
</IfModule>
For the other example with sport and league I wanted to find a solution in which I will manually add old link and new link, tried redirect 301, but I think it can't work, right?
Redirect 301 https://example.com/?sport=1&league=123 https://example.com/football/premier_league
I also tried Redirection plugin in WP, but it created Rule which destroys page and makes error 500:
RewriteRule ?sport=1&liga=123 https://example.com/football/premier_league [R=301,L]
or
RewriteCond %{QUERY_STRING} ^sport=1&league=123$
RewriteRule ^$ https://example.com/football/premier_league [R=301,L]
which gaved address: https://example.com/premier_league/?sport=1&league=123
I was also wondering if I can make it with pure php, since in PHP I can easilly get proper strings for id's of the sports and leagues. Unfortunantelly changing "?sport=1&league=32758" into "?sport=football&league=premier_league" and then clean it with rewrite in htacces is not possible right now.
I'm not expecting whole code for both problems, I believe that some directions would be enough. I browse stack and google and coudn't find any examples with links that looks like mine, so example.com/?sport=1 not example.com/page?sport=1
After further investigation I believe the problem is in the way I'm opening my custom pages. I'm using shortcodes to initiate a script and inside the script my php code is included with parameters. I think that because of that I got to this page https://example.com/premier_league/?sport=1&league=123 which was pretty close, but not working.
Now my question is how to send those parameters to proper script but not into url.

.htaccess redirect from URL with numeric parameter to URL with text parameter

I am having to move an old custom built website to a newer WordPress site.
On the old site, the URL is https://www.example.net/index.php?product=1234567890 and I need to redirect to https://www.example.net/product_name
Each product on the old site is a number parameter, and need to redirect to text on the new site.
So I figure I'll need a separate line in the .htaccess to accomplish this, but I don't know enough .htaccess.
Thanks.
Worked it out, as RewriteCond is like an If statement and RewriteRule is like a Then statement.
RewriteCond %{QUERY_STRING} (^|&)product=1314271738($|&)
RewriteRule ^index\.php$ /product_name? [L,R=301]
Very cool!

.htaccess: Condition on HTTP_USER_AGENT then rewrite with dynamic subdomain

I'm creating a static webpage solution for my angularJS project where I check for the HTTP_USER_AGENT in my .htaccess file and then redirect any crawlers to a static php page so I can add all the necessary meta data.
The problem is that I'm using a wildcard subdomain, so any of my clients can have their own subdomain and I can't figure out how to redirect via RewriteRule to the right url.
My RewriteCondlooks like this:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
and my basic non-working RewriteRule looks like this:
RewriteRule ^stuff/(.*)$ http://example.com/static.php?token=$1 [NC,L]
But I want it to go to static.php on that subdomain the request is on, so if I request http://subdomain.example.com/stuff?token=my-token is it possible to make the rewriterule go to http://subdomain.example.com/static.php?token=$1 instead of just the static mydomain.com above?
(there will NEVER be a request on http://example.com/stuff - all requests to stuff will be via a subdomain)
I'm not that experienced in .htaccess and get's a bit confused from all the answer that here listed at SO and Google finds.
I solved the issue by adding together pieces of code and a little bit of thinking.
This is the three lines in my .htaccess which does the trick for me:
RewriteCond %{HTTP_USER_AGENT} (facebookexternalhit/[0-9]|Facebot|Twitterbot|Pinterest|Google.*snippet)
RewriteCond %{HTTP_HOST} ^(.+?)\.example\.com$
RewriteRule ^s/(.*)$ http://%1.sociuu.com/static.php?token=$1 [NC,L]
If this can be done more elegant or smarter, please let me know otherwise I'll consider this as suitable for my needs :-)

Rewrite URLS in Wordpress site

I have some old SWF that has fixed links inside, so I need to rewrite all the links to redirect to new site I build on Wordpress.
I actually need to redirect all links that have FID=<123> to /me_<123> (that's the permalink I created for all pages I need to redirect)
For example:
http://kavor.org.il/KavOr07/Templates/showpage.asp?DBID=1&LNGID=2&TMID=843&FID=1997
Needs to go to http://kavor.org.il/me_1997
I tried to put this in .htaccess:
^FID=([^/.]+)/?$ /me_$1 [R]
but it has no effect and also for some reason slows down the entire site loading.
I know that Wordpress has its own rewrites so maybe it's interefering.
I would be glad for some help with that I got pretty lost in all the mod_rewrite rules...
To match query string you will need a RewriteCond with %{QUERY_STRING}
Try this rule:
RewriteCond %{QUERY_STRING} (^|&)FID=([^&]+) [NC]
RewriteRule ^KavOr07/?$ /me_%1? [L,R=302,NC]

redirecting urls that contain the "?" character in the file names

I'm a novice php/mysql developer and need some help.
I was working on a project with a .NET developer. He built a URL redirecting function in .NET that uses the ? in the URL (eg, mysite.com?123 redirects to www.realsite.com. This developer has now disappeared on me and I no longer have access to the server.
I need to move my domain to a new server and recreate this functionality in php/mySQL. I have about 40 urls that I need to support using this technique (they're embedded in QR codes). I don't need a solution that generates new redirects this way - I've got a different solution going forward.
My new server will be on bluehost.
Many thanks to anyone who can help me.
To replicate the original setup you dond't need to involve PHP at all. You can accomplish this at the server level using some RewriteRules:
RewriteCond %{QUERY_STRING} ^123$
RewriteRule ^$ http://realsite.com/
See also http://wiki.apache.org/httpd/RewriteQueryString
The query string can be found in $_SERVER['QUERY_STRING'].
On your old host for mysite.com domain create a .htaccess file in DOCUMENT_ROOT like this:
Options +FollowSymlinks -MultiViews
RewriteEngine on
# for HTTP traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =80
RewriteRule ^/?$ http://www.realsite.com/? [R=301,L]
# for HTTPS traffic
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{SERVER_PORT} =443
RewriteRule ^/?$ https://www.realsite.com/? [R=301,L]
Above code will redirect every URI that looks like mysite.com?foo to http://www.realsite.com/ with HTTP status code 301.
? in the end will discard any existing QUERY string.
http://example.com?123
would be the equivalent of doing
http://example.com/index.php?123
Your choices would be either to catch those codes via mod_rewrite and redirect the request to a script specifically set up to handle these legacy urls. Or you can modify the index.php script to look for the codes and do whatever it has to at that point.

Categories