htaccess mod rewrite - .htaccess

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.

Related

htaccces 1/2 similar rules not redirecting

I have two rewrite rules:
RewriteRule ^library/(.*)$ market-intelligence/resources/$1 [L,R=301]
RewriteRule ^library/.*\.pdf$ email/$1 [L,R=301]
As one can see, they are for the same directory, but the second deals with all pdf files. However, any pdfs in the directory still lead to the first rule's destination.
Am I doing something wrong?
Am I doing something wrong?
Yes. Rules are tested sequentially and with the [L], the first matching rule fires and quits that scan. So the trick is to order your rules from the specific to the general. In this case swap them. The PDFs will be rewritten to the email folder and the rest to market-intelligence/resource.

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]

mod_rewrite: How to disable not clean urls navigation of rewrite rules

I've been enabled mod_rewrite module and all is right.
I created simple rules for the url, but how do I disable the url navigation (rewritten) with the parameters?
example:
# rewrite rule for cleaning
RewriteRule ^bookstore/([0-9]+)?$ /bookstore/book.php?id=$1 [L]
Now, if I navigate to http://mydomine.com/bookstore/123 all is done, but the url http://mydomine.com/bookstore/book.php?id=123 is also navigable.
How can I make visible and bavigable only the first one?
Add this to the same htaccess file:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /bookstore/book\.php\?id=([0-9]*)
RewriteRule ^bookstore/book\.php$ /bookstore/%1? [L,R=301]
This will 301 redirect requests for the URI with query strings to the one without.
Not 100% sure about this, but I think that if you rewrite A to B, then both A and B will work.
I would like to ask why exactly is it a problem that http://mydomine.com/bookstore/book.php?id=123 is navigable too? What is the problem if that link is valid too, and the user can use both links... although it would take them some time and luck to discover the second option. What would they gain by doing that? What would you lose? If the answer in both cases is "nothing", then simply stop worrying. :) If you used the old links previously and now replace then with new links, then it is a good thing that your customer's old bookmarks will still work.
But assuming that you have a good reason for disabling the old URLs, how about changing them both. For example rename "book.php" to "xyz.php" and then redirect http://mydomine.com/bookstore/123 to http://mydomine.com/bookstore/xyz.php?id=123 -- and the old http://mydomine.com/bookstore/book.php?id=123 will stop working.
Ok, that is an ugly solution, but you can make it nicer if instead of renaming the files you just move them to a subdirectory, like http://mydomine.com/xyz/bookstore/book.php?id=123 . Alternatively, you could use the redirect to add a "secret" parameter and then check it in the PHP file, for example rewrite http://mydomine.com/bookstore/123 to http://mydomine.com/bookstore/book.php?id=123&secret=xyz . Sure, it's just a "security by obscurity", but again... what exactly would anyone gain by discovering your true URLs?

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.

How to apply different .htaccess rules based on the uri

I have a website running on ExpressionEngine and a custom backoffice based on Zend.
Both approaches expect a specific htaccess file placed in the root.
This is my structure.
http://localhost/... (ExpressionEngine pages, eg. http://localhost/calendar/events)
http://localhost/backoffice (Backoffice pages)
Before placing the backoffice on a subdomain, I wanted to know if it's possibile to have one htacces file and to apply a different set of htaccess rules based on the uri. So eg. everything with "backoffice" gets different rules.
I know you can do stuff like this:
Best I've found was the following Can I do an if/then/else in htaccess?
But I'm stuck with defined the variables based on the uri.
Thanks for your help.
regards
Vic
Put your rewrite rules for EE first and add a rule for the back office there:
RewriteCond %{REQUEST_URI} !/backoffice
RewriteRule ^ /index.php [L]
And now add the rules for the back office.

Resources