htaccess rewrite - removing characters from url - .htaccess

Looking to rewrite a part of a URL and stuck with dealing with special characters.
original
http://www.testwebsite.com/Products/Apple/*/!Accessories
desired result
http://www.testwebsite.com/Products/Apple-Accessories
I would also like to redirect (can a word be removed?) testwebsite.com/Products/Dell-Laptop/*/!Accessories
to testwebsite.com/Products/Dell-Accessories

You can use this rule in your root .htaccess:
RewriteEngine On
RewriteRule ^(Products/[^/]+)/\*/!(.+)$ /$1-$2 [R=301,L,NE]

Related

How to escape characters in wildcard in htaccess

I have a htaccess file which is used for earning money by short links, as the following code:
RewriteEngine On
RewriteRule (.*) http://shortlink.com/s/N9IpzM7J?s=$1 [R=301]
But with above code, it'll redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https:/google.com notice it that it lost a slash at https://, in my thought, it might be a special character in htaccess but I don't know how to escape it.
So I want to ask how to let the above code works which will redirect example.com/https://google.com to http://shortlink.com/s/N9IpzM7J?s=https://google.com?
Because of this post I replace by link shortening service's url to shortlink.com!
If you want to capture full URI, you should use RewriteCond directive.
Apache automatically strips off multiples slashes into a single slash in RewriteRule directive.
RewriteCond %{REQUEST_URI} ^/(.*)$
RewriteRule ^ http://shortlink.com/s/N9IpzM7J?s=%1 [R=301,L]

.htaccess rewrite rule, change URL

I'm looking for the following to change the URL with two variables in a .htaccess file, from:
domain.com/folder/a123/url/index.php
domain.com/otherfolder/a321/url/index.php
to:
domain.com/folder/url/index.php
domain.com/otherfolder/url/index.php
I would prefer it in a single line .htaccess RewriteRule, if possible.
"folder" and "otherfolder" are the only two "folder names" and "a123"/"a321" can be anything starting with "a" and ending with random numbers.
You can do that in one with just one rule using this:
RewriteEngine on
RewriteRule ^(.+)/a[0-9]*/url/index.php$ /$1/url/index.php [R=301,NC]
But if you want to redirect all pages and not only index.php then use this:
RewriteRule ^(.+)/a[0-9]*/url/(.*)$ /$1/url/$2 [R=301,NC]
Also, if you want to rewrite the URLs instead of redirect them change [R=301,NC] to [NC].
Note that if you have that pattern in other URLs this rule will also apply, for example it will also redirect domain.com/adifferentfolder/a567/url/index.php to domain.com/adifferentfolder/url/index.php, if you have those type URLs and don't want to redirect them you need to use two rules instead:
RewriteRule ^folder/a[0-9]*/url/(.*)$ /folder/url/$1 [R=301,NC]
RewriteRule ^otherfolder/a[0-9]*/url/(.*)$ /otherfolder/url/$1 [R=301,NC]

URL Redirection with character % in HTACCESS

I need to redirect multiple URLs with% character, through htaccess.
Currently the lines are:
RewriteEngine On
RewriteCond% {REQUEST_FILENAME}!-F
RewriteCond% {REQUEST_FILENAME}!-D
RewriteRule ^-.*$ url [R=301,L]
RewriteRule ^%20.*$ url redirection [R=301,L]
RewriteRule ^%22.*$ url redirection [R=301,L]
RewriteRule ^images/files/myname%is-fyle.pdf.*$ url redirection [R=301,L]
From these lines, the redirects do not work are:
RewriteRule ^%20.*$ url redirection [R=301,L]
RewriteRule ^%22.*$ url redirection [R=301,L]
RewriteRule ^images/files/myname%is-fyle.pdf.*$ url redirection [R=301,L]
Any ideas on what I'm doing wrong? My knowledge is basic almost, not quite understand why the redirects with % in the URL do not work.
I researched on the use of flags as noescape [NE], but I have not managed to get the redirects from the URLs with the character %.
("url redirection" in te lines of htaccess is the URL that being new to the forum I can not post).
Thank you.
Sep 23: I thank those who have helped me with this problem. However, the URLs with special characters (% and ?) and capital letters do not forward.
I copy and paste the complete code for my htaccess file:
Here: http://pastebin.com/yXGQiQzd
I saw in this post How to Redirect to a url containing an anchor (#)? that apparently the answer is posted. However, the matter is in the use of ([]) for the rule. The example is: ([a-zA-Z0-9-_]+)
In my case, for example, these URLs:
images/SchoolContry/wef/pdf/SomethingOfFiles/the reasons behind?s of the ca?da.pdf
images/stories/Money/memo Matric?cula 2010.doc
Any idea why URLs with special characters (% and ?), space and capital letters redirect not?
Thanks.
URL encoded characters get decoded before they're sent through the rewrite engine. That means you need to use the decoded characters to match against and not the encoded ones. So for:
RewriteRule ^%20.*$ url redirection [R=301,L]
you want:
RewriteRule ^\ .*$ url redirection [R=301,L]
and for:
RewriteRule ^%22.*$ url redirection [R=301,L]
you want:
RewriteRule ^\".*$ url redirection [R=301,L]

htaccess: Mediafire.com like urls

I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]

.htaccess 301 redirect path and all child-paths

I want accesses to e.g. www.thisdomain.com/docs/path1/path2 to redirect to www.thatdomain.com/path1/path2
(Note that docs is not a part of the new path)
I have the following on www.thisdomain.com:
RewriteEngine on
RewriteRule ^docs/* http://www.domain.com/ [R=301,L]
If I access www.thisdomain.com/docs, it directs to www.thatdomain.com, but if I access a child-path like www.thisdomain.com/docs/path1/path2 it fails. Is it possible for the redirect to intercept the child-path access and redirect as I need? If so, any pointers?
Thanks.
With regular expressions, * means any number of the previous atom, which will match /docs and /docs/. Try this:
RewriteEngine on
RewriteRule ^docs$ http://www.domain.com/ [R=301,L,QSA]
RewriteRule ^docs/(.*) http://www.domain.com/$1 [R=301,L,QSA]
(QSA is query string append, so /docs/foo?bar=baz won't lose the ?bar=baz.)
According to section "Redirect Old domain to New domain using htaccess redirect" of the first Google result which I found searching for "htaccess redirect" (without the double quotes), this piece of code will suffice:
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
According to their description of the technique, it will redirect the directory the .htaccess file is placed in recursively (including any child paths), just as you intend. Of course, mod_rewrite needs to be present for the rewrite directives to work.

Resources