exact syntax of RewriteRule - .htaccess

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 /.

Related

How to redirect url via .htaccess

I want to redirect via .htaccess
https://www.example.co/en/brand/Abc to https://www.example.co/en/brand/abc
I have tried
RewriteRule ^https://www.example.co/en/brand/Abc https://www.example.co/en/brand/abc [R=301,L]
The RewriteRule pattern (1st argument to the RewriteRule directive) matches against the path-part of the URL only, ie. /en/brand/Abc. An additional complication in per-directory .htaccess files is that the URL-path that is matched is also less the directory prefix (which always starts with a slash), so the URL-path does not start with a slash. In other words: en/brand/Abc (for an .htaccess file in the document root).
So, you will need to format the directive like this instead:
RewriteRule ^en/brand/Abc$ https://www.example.co/en/brand/abc [R=301,L]
(Assuming you already have RewriteEngine On defined and that this is near the top of your .htaccess file.)
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
You may try something like this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# Don't want loops
RewriteCond %{REQUEST_URI} !/abc
RewriteCond %{REQUEST_URI} /Abc
RewriteRule . https://www.example.co/en/brand/abc [R=301,L]
URL are usually case-sensitive. Check this document, while domain names are not. Therefore "abc" and "Abc" are not the same and that's what the question is about. I think.

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 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.

link redirection using htaccess RewriteRule

I have these links in my website:
www.example.org/folder/files.php?file=folder/document.pdf
www.example.org/folder/files.php?force&file=2009.pdf
and I want redirect to :
www.example.org/files/folder/document.pdf
www.example.org/files/2009.pdf
I tried :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
</IfModule>
but doesn't work!
any help?
RewriteRule ^files/(.*)$ /files.php?file=$1 [R=301,L]
There are two issues with this rule ... first, what you are matching needs to appear first in the rule, then what you are rewriting appears second - you have that backwards.
Once you reverse that, though, you run into the second issue - you can't match query strings in a RewriteRule, you need to match them in a RewriteCond:
To match www.example.org/folder/files.php?force&file=2009.pdf and redirect it to www.example.org/files/2009.pdf you would do:
RewriteCond %{QUERY_STRING} ^force&file=(.*)$ [NC]
RewriteRule ^folder/files.php$ /files/%1 [R=301, L]
The %1 matches what's in the parentheses in the RewriteCond.
Search on google first. The first thing displayed on google for htaccess is htaccess redirect. I think
Redirect /olddirectory/oldfile.html http://example.com/newdirectory/newfile.html (same line with a space) should work. Go to http://kb.mediatemple.net/questions/242/How+do+I+redirect+my+site+using+a+.htaccess+file%3F . Php would also do the work. Just goolgle things before asking them.

How do I remove a trailing slash?

I just want to remove a trailing slash from a directory. For example I want /p/page/ to show up as /p/page. It just looks better, doesn't it?
However I've tried many different kinds of mod_rewrites but none have worked or something happened.
I just want this to apply to subfolders (even better, any slash in a folder in a folder in a folder like /a/b/c), not /p/ as this may affect other parts of my site in a negative way.
You can try adding the following line to your .htaccess file:
DirectorySlash Off
That solved the problem for me a while ago. Of course if the path is only / I don't think you can get rid of it.
Copy this code in your root .htaccess file (directly under DOCUMENT_ROOT):
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L]
DirectorySlash Off
RewriteCond %{THE_REQUEST} ^GET\s(.*)/\s
RewriteRule ^ %1 [R=302,NE,L]
It will externally redirect http://localhost/blog/ to http://localhost/blog while still displaying default index.html or index.php or whatever under /blog directory.

Resources