Redirect URLs with Curly Brackets in it - .htaccess

I have about 30.000 old urls that need to be redirected
I am using excel to obtain a rewrite rule for each URL
My old URLS are using a Unique Identifier in the query string part
Basically I am trying to redirect
www.mysite.com/dettagli.asp?ID_S={965c1471-b985-45c1-9d7a-9fcede5711ed}
to
www.mysite.com/mynewurl.html
I can map any UID to the new URLS in excel but rewrite rules so far haven't been successful
I have tried escaping the cury brackets with "\" in front of them, no luck
So far I have tried
redirect 301 /dettagli.asp?ID_S={965c1471-b985-45c1-9d7a-9fcede5711ed}
and
redirect 301 /dettagli.asp?ID_S=\{965c1471-b985-45c1-9d7a-9fcede5711ed\}

Try to implement all redirect rules in php. The only thing you need set up in .htaccess in entry point for php.

actually it was not that easy, at least not for me.
I think I found a solution using:
RewriteCond %{QUERY_STRING} ^ID_S={2e69bb2d-655e-4488-b6cd-ba6e016db346}$
RewriteRule ^(.*)$ http://www.mywebsite.com/mynewurl.html? [R=301,L]
and so on....
but 30.000 Urls generate 60.000 Rows in htacces which weighs more than 5MB....
I think I 'll reconsider it

Related

Redirect 301 and Rewrite Rule Results in Query String being added

I know there are some conflicts with my redirect and rewrite statements, but I haven't been able to find a solution. I am not great with regular expressions.
Currently, I have this:
RewriteRule category_(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.html$ category.php?brand_name=$1&bid=$2&stid=$3&lid=$4&did=$5&sid=$6&sort=$7
It results in a page that similar to this:
category_Jackets-1-2-3-4-5-6.html
I want to now for SEO purposes, redirect that page to:
category_Womens_Jackets-1-2-3-4-5-6.html
I tried putting:
Redirect 301 /category_Jackets-1-2-3-4-5-6.html category_Womens_Jackets-1-2-3-4-5-6.html
But it resulted in:
category_Womens_Jackets-1-2-3-4-5-6.html?brand_name=Jackets&bid=1&stid=2&lid=3&did=4&sid=5&sort=6
I have tried using variations of Rewrite rule and Redirect to try to solve this, but no luck so far. I know the conflict is the issue, but I can't remove the original rewrite rule, or those pages would not work.
Any help would be appreciated.
The Redirect and RewriteRule directives are part of 2 different modules (mod_alias and mod_rewrite) and both will get applied to the same URI at different points. So you're rewriting AND redirecting at the same time. You should just stick with mod_rewrite to redirect if you're employing rewriting.
RewriteRule ^category_Jackets-1-2-3-4-5-6\.html$ category_Womens_Jackets-1-2-3-4-5-6.html [L,R=301]
The R=301 in the flags tells it to 301 redirect. You're going to want this before your other rule, and, your other rule is going to need to account for the Womens part.

.htaccess redirects: from old site with variables to Drupal & url aliases

Please look at the edits on this post. The last code examples I give are what I'm trying to accomplish. I have 147 lines of code that I'm working with. I guess I'm actually not trying to make a rule so much as I am trying to figure out redirects with those variables.
OK, I've been researching this for a few hours, and the regex apache rewrite rules get pretty specific and I'm stumped. Client has asked that the old site urls be rewritten/redirected to the new drupal site and corresponding pages that have url aliases. There are 147 variables (all 1 - 216 (out of sequence))
Example:
I need
/hardware/bikes.php?recordID=1 to redirect to http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
then
/hardware/bikes.php?recordID=2 to redirect to http://www.bikerus.com/?q=hardware/second-awesome-bike-that-really-flies
and so on
This is what I have so far:
RewriteCond %{QUERY_STRING} ^recordID=([0-9]+)$
RewriteRule ^/hardware/bikes\.php?recordID=%1$ http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
This is obviously not working, any ideas ladies and chaps?
Ideally, it would be great if I could figure out another problem with Drupal I haven't figured out yet (for years).
Have a redirect that goes like this:
redirect 301 /hardware/1.html http://www.bikerus.com/hardware/first-bike-that-really-flies
Instead of this:
redirect 301 /hardware/1.html http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
But my first question is the priority now.
Thanks in advance.
EDITS:
The files I'm trying to redirect to aren't static, they're url aliases generated by Drupal's database.
In the end, yes, I will need to make 147 individual records (hardcoded), my question is how to do this:
redirect 301 /hardware/bikes.php?recordID=1 http://www.bikesrus.com/?q=hardware/random-bike-title-one
redirect 301 /hardware/bikes.php?recordID=2 http://www.bikesrus.com/?q=hardware/random-bike-title-two-ewhbcfn
redirect 301 /hardware/bikes.php?recordID=3 http://www.bikesrus.com/?q=hardware/random-bike-title-three-kxjhmuflr
redirect 301 /hardware/bikes.php?recordID=4 http://www.bikesrus.com/?q=hardware/random-bike-title-four-more-random-stuff
And for those that don't know, Drupal has a clean url rewrite rule that comes shipped with the .htaccess file that I'm currently editing:
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
So I need to be able to navigate this as well.
The problem to your second issue is that redirect really stinks for anything past a simple redirect. You need a RewriteRule to make this:
redirect 301 /hardware/1.html http://www.bikerus.com/?q=hardware/first-bike-that-really-flies
More like this:
RewriteRule ^([a-zA-Z]+)/$ /?q=$1 [R=301,L]
Or maybe this:
RewriteRule ^([a-zA-Z]+)/(.*)$ /?q=$1/$2 [R=301,L]
But it’s not clear in your post where you are getting the first-bike-that-really-flies stuff from. So not to clear on how to handle that in Drupal. Very specific to your setup.
Actually just answered a mod_rewrite question similar to this over here. Might give you more insight.
Just figured it out.
RewriteCond %{THE_REQUEST} recordID=1
RewriteRule . http://www.bikesrus.com/hardware/bike-that-really-flies? [R=301,L]
BOTH!! On my own, with a little more searching. The difference is that I put the word "hardcoded" into my search keywords this time. Thanks everyone who commented.

How to write .htaccess 301 redirect rule for

I need to redirect some urls.
I need to direct www.site.com/city-st-key-word-string to www.site.com/city-st/key-word-string.
I have about 500 cities I need to do this for. Preferably instead of making a redirect rule, I would like to change the keyword string as well. However, I will end up having 2,500 redirects.
Is it ok to have $2,500 redirects or should I use a redirect rule and if so, what would it be?
Thanks for your help in advance!!!!
Would this keyword string be the same for all cities? If yes, then you could create one rule and change the keyword string in the rule (and it would reflect on all redirects).
Individual rewrite rules would look like this -
Redirect /city-st-key-word-string http://www.site.com/city-st/key-word-string
A single rewrite rule for any city-st would look like this -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-key-word-string$ $1/$2/key-word-string [R=301,L]
A rewrite rule redirecting any /x-x-??? to /x-x/??? (i,e; splitting at the two hyphens) -
RewriteEngine on
RewriteRule ^(.*)/([a-zA-Z]+)-([a-zA-Z]+)-(.*)$ $1/$2-$3/$4 [R=301,L]
Hope this helped :)

How do I do a Htaccess 301 redirect with dynamic files

I've tried many many methods of this but none seem to work.
I upgraded my site to PHP from ASP and as a result now I have 300+ 404's which i should of seen coming, but oh well.
Problem is I need to redirect these files and they are all dynamic:
old files are like: http://www.domain.co.uk/resoucecentreDetails.asp?title=foobar&ID=37
There are literally 100s of these so is there a quick way I can redirect them to a single page i.e. http://www.domain.co.uk/resoucecentreSelect.php
You can use Rewrite Rules in .htaccess.
Write following Rule in your htaccess.. It will redirect all your dynamic URLs to one page..
RewriteRule ^resoucecentreDetails.asp?title=(.*)&ID=(.*)$ /resoucecentreSelect.php [L,R=301]
This rule will do the job:
# activate rewrite engine
RewriteEngine On
RewriteBase /
# redirect rule
RewriteRule ^resoucecentreDetails.asp$ http://%{HTTP_HOST}/resoucecentreSelect.php [NC,QSA,R=301,L]
It will redirect all requests from /resoucecentreDetails.asp to /resoucecentreSelect.php preserving the query string, e.g.:
http://www.domain.co.uk/resoucecentreDetails.asp?title=foobar&ID=37
will become
http://www.domain.co.uk/resoucecentreSelect.php?title=foobar&ID=37

htaccess redirect with dynamic variables conflict

I'm working in an old CMS that uses htaccess to rewrite URIs with GET variables into something more user friendly.
RewriteEngine on
RewriteRule ^animals/(.*)/ secondary.php?page=$1
RewriteRule ^animals/(.*) secondary.php?page=$1
which results (correctly) in
http://www.example.com/animals/duck
The problem is I now need to redirect some of those pages to new pages. I've tried:
Redirect 301 /animals/goose http://www.example.com/animals/fowl
The redirect almost works, but it adds "?page=goose" to the end of the rewritten URI:
http://www.example.com/animals/fowl?page=goose
I've tried using RewriteRule as well as RewriteCond, but unfortunatley I'm having no luck. Any help would be immensely appreciated.
Try placing this before the other rules instead of the Redirect statement. R=301 is for the redirect and L signals that the rule in question is the last rule to be processed.
RewriteRule ^animals/goose /animals/fowl [R=301,L]
Also you can easily make the slash (just like any other character) optional with a question mark, instead of having two rules.
RewriteRule ^animals/(.*)/?$ secondary.php?page=$1

Resources