Rewriting rules in htaccess to add hyphens between names - .htaccess

I am not sure if this is even possible, but am trying to rewrite some urls in my htaccess file using one line of code rather than to add a separate rewrite for each of my hundreds of urls.
The existing urls are like the one below where the car make and model is in one string.
www.exampledomain.co.uk/cars/astonmartindb9/
I want to separate the make and model by adding a hyphen as below
www.exampledomain.co.uk/cars/astonmartin-db9/

This will do it:
RewriteEngine on
RewriteRule ^cars/(astonmartin|ford|renault)([^/-]+)/$ /cars/$1-$2/ [R=301,L]
You need to list all the different makes, separated by pipe characters, as shown. Should be the simplest solution.

Related

.htaccess rewrite for orphaned URLs containing underscores and arguments

I only modify the .htaccess with great care for the purposes of my online store.
Some time ago, I did a website migration from osCommerce to OpenCart. This resulted in orphaned osCommerce-style URLs with these two example formats:
http://www.londonpower.com/catalog/product_info.php?products_id=75
http://www.londonpower.com/catalog/product_info.php?cPath=15&products_id=75
Lots of websites in internet-land have links to my old-style URLs, and I have about 100 of them, so I would like to redirect them to new URLs with the following format:
http://www.londonpower.com/2-channel-guitar-preamp
If I understand correctly, the problem has two parts:
to eliminate the underscores, as they baffle the .htaccess engine;
to then perform a 301 redirect on the URL.
So far, I have been able to get the first underscore to change to a hyphen, with this Rewrite Rule:
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
...but no luck with the second underscore (the one that is part of the query string after the "?"). I am stuck there.
I would avoid using rewriting for this. Does the file catalog/product_info.php exist in the new store? If not, create it and add a simple redirection using a map of old IDs to new URLs. If so, do the same thing in a different file, like old-redirector.php then rewrite requests to it.

htaccess random redirect to random url

is it somehow possible to randomly redirect purely in htaccess? (without need of php)
Something like:
rand_pool() = ['a.php', 'b.php', 'c.php']
RewriteRule ^page.php$ /rand_pool() [R=301,L]
You can use RewriteMap directive for this.
First you need to generate a map file:
## random.map -- rewriting map
randomX a.php|b.php|c.php
You can generate as many values (separated by |) as needed, however I don't know if there is any limit in apache regarding their count.
Then you define the Randomized Plain Text type of rewrite map using this file and finally use it in your rewrite rule.
RewriteMap randomMap rnd:/path/to/random.map
RewriteRule ^page\.php$ /${randomMap:randomX} [P,L]
No, this is not possible, at least not with any amount of efficiency. One might be able to abuse mod_unique_id to generate an unique id, then reduce this id to a new page, but this is at best dodgy.
Instead internally rewrite to a router page, then perform the necessary redirect on this page. When maintaining the code, it will be much more clear what you wanted to achieve with this.

htaccess folder organization and rewrite

I am having trouble figuring out how to work a rewrite, or even if it is a good idea.
I would like to name my folders in my webroot the following way for the sake of organization:
_fol.A
_fol.B
_fol.C
But I would like, using .htaccess mod_rewrite, people to reach the above folders when they visit
http://example.com/Alpha
http://example.com/Bravo
http://example.com/Charlie
Is this just over-complicating my life so that I can have my folders all appear together?
I think it's valid to group the folders in the physical _fol. I do this with products, where the url will be simply the product name /alpha, but they are in a products/alpha folder. This separates them nicely from the rest of the stuff on the website.
The other comments address your question of whether you're over-complicating, e.g., consider using the final names, and just put them down in the folder for organization.
That said, to answer how to do it, as CBroe said in the comments, RewriteMap is the answer.
This is one that doesn't rely on any particular url pattern - the product itself is the entire url. The RewriteCond line is making sure that it is in the map file. Keep or remove the leading slash in front of _fol as needed.
RewriteMap map_folder txt:d:\somepath\map_folder.txt [NC]
# Map folders.
RewriteCond ${map_folder:$1|NOT_FOUND} !NOT_FOUND
RewriteRule (.*) /_fol.${map_folder:$1} [NC,L]
The map_folder.txt file has entries like these:
Alpha A
Bravo B
Charlie C
If it's some other url, like /help, then it won't be found in the map file, and will fall through to further rules.

htaccess to rewrite param=NUMBER into /text

I am trying to figure out how to rewrite URLs from something like this:
example.com/collection.php?collection=1
Into:
example.com/collection-name.php
I recently redesigned an e-commerce site and need to redirect the old URLs to the new ones. I've seen loads of instructions on how to use the value of collection=1 in a rewritten URL but not how to use text instead of the value. There are only 5 collection values that need to be redirected but in addition there are also old URLs that have multiple params in them that also need to be rewritten/redirected as text. I hop that made sense.
So I think I can get them all worked out if I can get the initial redirect set up.
Other/more complex old URLs look like this:
example.com/collection.php?collection=1&product=2&item=3
Which would then need to be redirected to:
example.com/collection-name/sub-collection-name/product-name.php
Not sure exactly how to go about doing this. I don't want to write line after line in the .htaccess file but I have no idea of how else to accomplish this.
Thanks in advance, I appreciate andy and all help in this matter!
EDIT------------------------------------
Here's my new rewrite condition and rule based on the info provided to me. This would be the rule for the URLs containing all the query params using 3 separate RewriteMaps.
RewriteCond %{QUERY_STRING} collection=([^&]+)&product=([^&]+)&item=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}/${rangemap:%2}/${productmap:%3}\.php [R=301,L]
Please let me know if anything looks off or I missed anything. Wasn;t sure if I needed to use $1, $2, $3 for each of the query params. I'm still a NOOB with rewrites. Thanks!
Edit------------------------------------------------------
Using the following code in my .htaccess file:
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php shop/${categorymap:%1}\.php [R=301,L]
My URLs that start as "example.com/collection.php?collection=1
Are being rewritten as: example.com/shop/.php?collection=1
I am a bit lost on this one. Could it be that my Redirect Maps are not being read? Or is it something else? Thanks for the help.
You want to look into using a RewriteMap. You can specify the maps you want for collection, sub-collection and products, and then look up the text for each number.
So you would define a RewriteMap in your virtualhost config with something like
RewriteMap categmap txt:/path/to/category/map.txt
Then your rewrite rule would look something like
RewriteCond %{QUERY_STRING} collection=([^&]+)
RewriteRule collection.php ${categmap:%1} [L,R]
Add more RewriteConds for your more complicated cases and make separate rules out of them. Place the more specific rules first in the file, then the more general ones.

htaccess removing index.php and search queries from URL

I have a site that, for a certain php function to work, needs the url to be:
/topic/index.php?height=###
I would like the URL to read
/topic/
What can I put in the .htaccess file to achieve this? Can I put one htaccess file in the root, or would I need to put one in each /topic/ directory?
I think the following might work for your issue:
RewriteRule ^([^/]*)/?$ $1/index.php?height=###
Of course, that's assuming a static number. If you need a dynamic number or one provided by the client, you're going to need something like:
RewriteRule ^([^/]*)/(\d+)/?$ $1/index.php?height=$2

Resources