How to escape characters in wildcard in htaccess - .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]

Related

htaccess rewrite rules with regexp and inifite redirect

I have a hard time to create rewrite rule for a redirect using part of an old URL for WP. Example:
Old URL:
http://www.example.com/news/index.php/2014/11/07/my-blog-post-from-old-site
or
http://www.example.com/news/index.php/2014/11/07/my_blog_post_from_old_site
New URL:
http://www.example.com/2014/11/07/my-blog-post
New URL should to have only dates and first three elements of a permalink after stripping from dashes.
My solution came after combining answers from here https://stackoverflow.com/a/32852444/1090360 and here https://stackoverflow.com/a/1279758/1090360
Somehow part for replacing underscores with dashes creates infinite redirect and a server freezes. If I will remove part with replacing underscores to dashes all the rest works as should.
Here are my .httaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#replace underscores with dashes
RewriteRule ^(/news/.*/[^/]*?)_([^/]*?_[^/]*)$ $1-$2 [N]
RewriteRule ^(/news/.*/[^/]*?)_([^/_]*)$ $1-$2 [R=301,L,NC]
#redirect to new URL
RewriteRule ^news/index\.php/([^-]+-[^-]+-[^-]+).* /$1 [R=301,L,NC]
#WP standard stuff
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I think, this is an expensive way to replace underscores with dashes. But this works at least in my test environment. The first rule replaces dashes one by one. The second rule then removes the prefix from the requested URL.
RewriteBase /
# replace underscores with dashes
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [L]
# strip "news/index.php"
RewriteRule ^news/index.php/(.*) /$1 [R=302,L]
I played a bit more with your original approach using the N|next flag and crashed my server too. Looking into the error.log, it seems this infinite loop is created by Apache by adding a "path info postfix", which enlarges the URL with the original URL. And so it keeps replacing underscores with dashes on and on.
You can prevent this path info postfix with another flag DPI|discardpath, which gives the following rule
RewriteRule ^(news/index.php/.+?)_(.*) $1-$2 [N,DPI]
This seems to work too. Although I must admit, I don't really understand this "path info postfix" thing. There's also an entry in Apache's Bugzilla, Bug 38642: mod_rewrite adds path info postfix after a substitution occured
Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

htaccess rewrite - removing characters from url

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]

htaccess rewrite querystring and remove empty value

first, sorry for my bad English.
I try to rewrite url generated from Form Get and redirect that.
my url is like this:
http://www.mysite.com/properties?action=search&agreement=for-rent&category=my-category&type=&zone=my-zone&city=my-city
and I have this .htaccess configured:
11. RewriteCond %{QUERY_STRING} ^action=(?:[a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)&(?:.*)=([a-zA-Z\-]*)$
12. RewriteRule (.*) %{REQUEST_URI}/%1/%2/%3/%4/%5/? [R=301,L]
So basically all my request are direct to index.php.
21. RewriteCond %{REQUEST_URI} !index\.php|resources|hidden
22. RewriteRule ^(.*)$ index.php/$1 [L]
All works, but the problem is when I have an empty value in query string, the rule add double slash and the above url (for example whit &type=&zone=my-zone... type have empty value) will translate like that:
http://www.mysite.com/for-rent/my-category//my-zone/my-city/
The question is: How can i remove in .htaccess the double slash generated if i have one or more empty value in query string?
Thanks
Easiest is to do another redirect (not real pretty as it requires two 301's).
RewriteCond %{THE_REQUEST} //
RewriteRule .* $0 [R=301,L]
The fun part is that when the url is loaded with a double slash in it, mod_rewrite will automatically remove this. So as you can see above you'll just have to rewrite the url to itself, kind of.

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]

exact syntax of RewriteRule

Do I need a slash before or after the /somefolder/(.*) below? I want to redirect everything coming into a folder to another website address. Do I need the RewriteCond in here somewhere also?
Options +FollowSymLinks
RewriteEngine on
RewriteRule somefolder(.*) http://www.differentsite.com$1 [R=301,L]
You don't need a / at the beginning, but a caret ^ will match http://whatever.com/ including the trailing /.

Resources