Htaccess rule to turn /home/index/2 into /page/2? - .htaccess

Now my urls look like this:
http://mysite.com/home/index/2
What would be the htaccess rule to turn that into this?:
http://mysite.com/page/2
Note: 2 can be any number starting from 1 to infinity.

I refer to this website for reference every time I need to rewrite the url. You should probably read there too. Below is my suggestion, please modify according to your need.
RewriteRule ^/home/index/(.+) /page/$1 [R]

Related

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

Rewrite Rule - add not precised number of parameters to a rule

is it possible to make rule that works like this:
example.com/param1val/param2val/param3val/...
And as follow, many params. I can't know how many of them would be there, but I want to rewrite it to:
example.com/index.php?param1=param1val&param2=param2val&param3=param3val...
and so on. Is it possible in one rule?
As per my comment this answer is for case 1):
RewriteEngine On
## reucrsion based rule to convert /user/n1/v1/n2/v2 to /user.php?n2=v2&n1=v1
RewriteRule ^(index\.php)/([^/]*)/([^/]*)(/.*)?$ /$1/$4?$2=$3 [L,QSA,NC]

htaccess mod rewrite

I want to rewrite the newsletter page of my site to url.com/newsletter/ the problem is, I have another rule overlapping this step. The rules looks like this :
RewriteRule ^([^/]*)/$ /index.php?categories=$1 [L,QSA] //Primeryrule overlapping the secondary rule
RewriteRule ^newsletter/$ /?newsletter=$1 [L]
Is there any possibility to apply special case rules or something like that (I don't want to use any workaround like .html or .php extension or stuff like this, just the url as above).
Apache reads these rules from the top down. So put your new rule first and then the existing rule and give it a try.
Just found the solution, that I need to keep the newsletter rule on top of the other rule to make it apply first.

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 301 mod rewrite matching url pattern

This one's beyond my ability I'm afraid and after much research and breaking things I've come for help. I have a site with a URL in the format of:
example.com/7865/travel-photo/my-amazing-photo
My client has now decided he'd like to add an 's' onto photos so the URL has become:
example.com/7865/travel-photos/my-amazing-photo
All great and that alone wouldn't have been a problem only for the fact that the number '7865' changes for each URL just like the 'my-amazing-photo' does. The basic structure of the new URL needs to be:
example.com/'number variable'/travel-photos/'article name variable'
So my question how does write a beautiful htaccess mod rewrite rule for that?
RewriteEngine On
RewriteBase /
RewriteRule ^([0-9]+)/travel-photos/(.*) $1/travel-photo/$2

Resources