htaccess multiple directory redirect - apache

I would like to know how you can filter out a multiple list of URL, and redirect to a page .
Example:
www.domain.de/notuse/seite1.html
www.domain.de/dir1/notuse/seite1.html
www.domain.de/dir1/dir2/notuse/seite1.html
I want now " notuse " have filtered and since I am total beginner , I'm not sure if that is correct :
RedirectMatch permanent ^.+notuse/ http://www.domain.de/new-directory/index.html
What would you suggest?

change your regex patterns :
RedirectMatch ^/.*notuse/site1\.html$ http://domain.de/new-dir/index.html
.* matches any charecters zero more times, so here it would match /dir1/dir2 if present in Request_uri.

Related

How to rewrite URLs in htaccess that end with recurring characters

I have changed web platforms and have old URLs that I cannot and do not want to match on the new platform where the old content is now living.
I have an array of old product URLs that all have '-p-' in the URL, followed by a string of numbers and ending in .html (osCommerce platform URLs).
I would like to know how to rewrite:
/x/[rest-of-url]-p-[random numbers].html
to
/x/[rest-of-url]
I would like the end result to look something like this:
http://www.shop.com/shop/versace-black-snakeskin-pony-hair-hobo-p-2214.html
redirects to:
http://www.shop.com/shop/versace-black-snakeskin-pony-hair-hobo
Does anyone know if this is doable in the htaccess file as a rewrite rule?
My managed hosting service providers BeepWeb answered my question.
RewriteRule ^/shop/(.*)-p-(.*).html$ http://www.shop.com/product/$1/ [R=302]
The first argument is the URI that you are matching. The (.) matches any characters. The second argument is the destination URL. The $1 corresponds to the first (.). $2 would be the second (.*), and so on... The [R=302] tells the rewrite to be a 302 redirect (use [R=301] for a 301 redirect).
Using the (.) is essentially like using a wildard. You can instead narrow this down by specifying which characters you want to match as opposed to all characters (instead of using (.) you could use ([abc]*) which would match only against a, b and c characters).
Also, be careful that you do not match other URLs unintentionally (i.e. you need to make sure that the pattern matches are unique to the URLs being rewritten).
If you need the source reference, see the following:
https://httpd.apache.org/docs/current/rewrite/intro.html
Thanks again to http://www.beepweb.com for their detailed response.
Hope it helps others.

How to capture groups in htaccess?

I'm having a hard time capturing groups in htaccess with a 301 redirect.
My rule is RedirectMatch 301 ^/viewitems/([_\-a-zA-Z0-9]+)/([_\-a-zA-Z0-9]+) http://example.com/catalogsearch/result/?q=$2&dir=desc&order=relevance
but with a test url of
http://example.com/viewitems/underground-hardwar/manhole-cable-hooks
it builds a url that looks like this:
http://example.com/catalogsearch/result/?q=manhole-cable-hooks/viewitems/underground-hardwar/manhole-cable-hooksdir=desc/viewitems/underground-hardwar/manhole-cable-hooksorder=relevance
Apparently, replacing every & with every capture group. What is going on and what am I doing wrong?
Apache uses PCRE so I believe the unescaped & does what the perl does with $& which is, it holds the string of the last pattern matched, in this case:
/viewitems/underground-hardwar/manhole-cable-hooks
So what happens is that for each & it replaces it with the above.
That's why the & needs to be escaped to give you the desired result.
RedirectMatch 301 ^/viewitems/([^/]+)/([^/]+)/? /catalogsearch/result/?q=$2\&dir=desc\&order=relevance

rewrite spaces to redirect to underscores when following a specific domain url string and only

how to write a modrewrite that will replace a space %20 with an underscore that will look like this. http://cityinsider.com/b/ocean%20shores_wa/mikes-seafood-ocean-shores will redirect to http://cityinsider.com/b/ocean_shores_wa/mikes-seafood-ocean-shores . All the underscores are permanent, but sometimes there is a space between two and three word cities. so any spaces found after only this type and part of url: cityinsider.com/b/ need to be replaced . So this won't effect any other url strings that don't look like the above; for example it should not work on spaces that are immediately after domain root e.g. cityinsider.com/%20/appleWood and shoulnd't work where it doesn't follow the cityinsider.com/b/ such as cityinsider.com/c/%20
Try:
RewriteEngine On
RewriteRule ^([^/]*)\ ([^/]*)/(.*)$ /$1_$2/$3 [L,R=301]

How to replace escape codes in url and redirect it using htaccess?

I need to redirect multiple urls from this format:
http://site.com/gallery.php%3Fpage%3D12
(the 12 at the end is the page number, i have many links like this with different numbers at the end)
to this:
http://site.com/gallery.php?page=12
how to i write a rule in htaccess that will replace those chars in all the urls and redirect them to the correct urls?
By default URLs in mod_rewrte are decoded(unescaped) so there is no need to escape(encode) them!
As mentioned by "Death", there is no need to replace the chars, this simple rule did the trick:
RewriteRule ^gallery\.php\?page\=(.*) http://site.com/gallery.php?page=$1 [R=301,L]

mod_rewrite: do not apply here

I am using mod_rewrite to put a category name in the URL, like locahost/categoryName and also a location localhost/categoryName/locationName .
One question I had, and I'm sure it's easy to do as a rewrite rule, is how can I make it so that the rule does not apply to some specific directory, like: localhost/admin . In that case I want it to go to the actual physical directory.
Thanks
Make this your first rule:
RewriteRule localhost/admin - [L]
That means: match localhost/admin, do nothing, last rule (only if matched).