I'm curious to know if there is any difference in speed between RewriteRules and Redirect within a .htaccess rule on Apache.
To my mind, RewriteRules can often be complex regex expressions which I assume have overhead (even if it's incredibly small) compared to Redirect that would be simple string matching(?)
So, if I had:
RewriteRule ^mytestpage\.html$ http://www.google.com [R=301, QSA]
vs
Redirect 301 /mytestpage\.html http://www.google.com/
I'm probably never going to notice a difference, but what if I had 1000 unique redirects? or 10,000? Would it be adventagous to use one over the other?
The speed implications of using either is negligible and you won't notice a difference. That being said, you should use the right tool for the job.
Doing a simple redirect, you should use a Redirect instead of using Mod_Rewrite. That example is something a Redirect should take care of. When you need to start doing more complex things you can think about using Mod_Rewrite.
Even with 1000 or 10,000 redirects you're not really going to notice a big difference. However it will use more RAM. Probably a few MB's if that.
So to answer your question, it really wont have a real impact but use the right tool for the job.
This should help.
When not to use mod_rewrite
As PanamaJack mentioned the link, Apache Docs say that:
mod_alias provides the Redirect and RedirectMatch directives, which provide a
means to redirect one URL to another. This kind of simple redirection of one
URL, or a class of URLs, to somewhere else, should be accomplished using these
directives rather than RewriteRule
So , I understand it like this: for better speed use Redirect/RedirectMatch (mod_alias) rather than RewriteRule (mod_rewrite).
Related
I know similar questions have been asked before, but I've been unable to locate one similar to my situation.
I have requests being made on our Joomla site of the form:
/news/privacy/how-2018-became-facebook%C3%A2%C2%80%C2%99s-worst-year-in-privacy-and-security
First, is this a valid URL? If not, then my interest here would only be in figuring out how to avoid a redirect loop in general with a URL involving a regex.
This appears to be due to Microsoft special characters in the title of the article. I'd like to create a rewriterule with a regex that redirects the user to the proper URL.
RewriteRule /news/privacy/how-2018-became-facebook.*s-worst-year-in-privacy-and-security /news/privacy/how-2018-became-facebooks-worst-year-in-privacy-and-security [L,R=301]
However, the above just causes a redirect loop. I've also tried replacing all the above encoded characters with dots and the browser just reports URL invalid. I thought the L flag was enough for it to not further process any rules, including itself. Perhaps I need a RewriteCond?
These URLs are also mostly generated by bots. I think they are guessing the logical URL based on the title of the article, while the actual URL is what appears in the substitution provided above. We're working on eliminating these titles with Microsoft special characters in them, but for the time being, we'd like to create an appropriate substitution. It's also a learning exercise for me.
These RewriteRules would be created based on entries from the access_log, so we'll have the exact pattern for each, but I would like some general guidelines I can follow to prevent RewriteRule loops such as the one above.
The actual URL is here:
https://linuxsecurity.com/news/privacy/how-2018-became-facebooks-worst-year-in-privacy-and-security
You can see when the article was created the title was created using a Microsoft special quote character.
You may be able to use this rule for this redirect:
RewriteRule ^(news/privacy/how-2018-became-facebook).+s(-worst-year-in-privacy-and-security)/?$ /$1s$2 [L,R=301]
I'm a real beginner with regex. I think I NEARLY understand this, but having spent a day and a half on it, I can't quite get it right.
At the moment my OLD domain has this rewrite:
RewriteRule ^/?$ "http\:\/\/sheepdog\-training\.com\/" [R=301,L]
That successfully redirects every page on the old site to the exact same file on the new site (sheepdog-training.com). Unfortunately, I need it to redirect to two new sites.
I need all files which begin with /tb- to go to: sheepdog-training.com and I need (nearly) all the remaining files to go to: theworkingsheepdog.com.
The exceptions are: contact and main-menu.
My web hosting company says I should do a separate 301 redirect for every page and post on the website, but there are hundreds of them! I'm sure what I'm looking for is possible with regex. Can you help please?
You will have to have multiple RewriteRules (remember that the rewrite conditions apply to the rule that immediately follows them thought).
Also remember: You should avoid using .htaccess files completely if you have access to httpd main server config file. Using .htaccess files slows down your Apache http server. Any directive that you can include in a .htaccess file is better set in a Directory block, as it will have the same effect with better performance.. See here: https://httpd.apache.org/docs/current/howto/htaccess.html
RewriteRule ^/tb-(.*)$ "http\:\/\/sheepdog\-training\.com\/$1" [R=301,L]
RewriteRule ^/?(.*)$ "http\:\/\/theworkingsheepdog\.com\/$1" [R=301,L]
Something like this should redirect all the paths starting with /tb- to the same path (except for the /tb- part) to the sheepdog-training site. And the other rule should redirect all the other URLs to the workingsheepdog site. If the first rule matches, the rule processing stops, so order of these two rules is important.
Configs are not tested by me, but should require only small modifications if any in case they do not work.
I have a website that is getting a lot of requests for pages that don't exist.
All the requests are based on an existing page, but have RK=0/RS= plus a random string of characters at the end.
For example, the request is:
www.domain.com/folder/article/RK=0/RS=M9j32OWsFAC_u8I6a0xOMjYKU_Q-
but the page www.domain.com/folder/article does exist.
I would like to use htaccess to say:
if RK=0/RS= exists, remove it and everything after
but haven't been able to get it working.
All the htaccess rules talking about removing query strings, but I'm guessing because this doesn't have a ? it's not a query.
Could someone help me understand how to do this?
Someone found where this mess is coming from.
http://xenforo.com/community/threads/server-logs-with-rk-0-rs-2-i-now-know-what-these-are.73853/
It looks like actually NOT malicious, it's something broken with Yahoo rewrites that create URLs that point to pages that don't exist.
The demo described on xenforo does replicate it, and the pattern of the URLS that Yahoo is producing:
http://r.search.yahoo.com/_ylt=A0SO810GVXBTMyYAHoxLBQx./RV=2/RE=1399899526/RO=10/RU=http%3a%2f%2fkidshealth.org%2fkid%2fhtbw%2f/RK=0/RS=y2aW.Onf1Hs6RISRJ9Hye6gXvow-
Sure does look like the RV=, RE=, RU=, RK=, RS= values are of the same family. It's just that somewhere the arg concatenation is screwing up on their side.
You can use this rule in root .htaccess file:
RewriteEngine On
RewriteRule ^(folder/article/)RK=0/RS= /$1 [L,NC,R=301]
Essentially my problem is thus; I have a MVC system that redirects all requests to index.php on my site. I have a rewrite rule in my htaccess file to handle those requests like so:
RewriteRule ^([a-zAZ\_\-]+)\/([a-zA-Z\_\-]+)\/([^\/?]*) /?module=$1&class=$2&event=$3
Which translates urls into these type of urls
http://example.com/users/login/
http://example.com/users/info/me
My problem is that I also want GET variables to be applied and used in the URL like so
http://example.com/users/login/?var1=val1&var2=val2
http://example.com/users/info/me?var1=val2...
I've written two different regexes that work perfectly well in a my workbench (expresso) and I've tested them out in PHP however they refuse to work in htaccess. They're not particular complex, I have tried:
^([a-zAZ_\-]+)\/([a-zA-Z_\-]+)\/([^\/\?]*)[\?]*(.*) /?module=$1&class=$2&event=$3&$4
and
^([a-zAZ_\-]+)\/([a-zA-Z_\-]+)\/([^\/\?]*)(?(?=\?)\?(.+)) /?module=$1&class=$2&event=$3&$4
Neither of these work and I'm racking my brains as to why. Essentially it just doesn't recognise the fourth group and returns nothing I thought it might have been due to it being next to an ampersand but I did &var=$4 as a test and it still fell over.
Any help with this would be greatly appreciated as this is driving me insane.
Thanks in advance,
Rupert S.
After all, this is what you need:
RewriteRule ^([a-z_-]+)/([a-z_-]+)/([^/?]*) /?module=$1&class=$2&event=$3 [QSA,NC,L]
[QSA] will append the additional GET parameters to the rewritten query string.
[NC] since it is case insensitive, no need for A-Z matches
My question is a simple one, but I can't seem to find the answer. I'm currently working on some URL rewriting for a website, but I have encountered a problem. Currently the most basic rule I have goes something like this:
RewriteRule ^([a-zA-Z]+)/(([a-zA-Z]+)/?$ index.php?mod=$1&com$2
This works in most cases, and I have some special cases for where this doesn't apply, however one of the pages needs to pass a lot of information through the URL, and I want to automatically rewrite this. Some examples:
website.com/asdf/jkl/id/5/page/2 should become website.com/index.php?mod=asdf&com=jkl&id=5&page=2
website.com/qwer/yuio/search/keyword/sort/alpha should become website.com/index.php?mod=qwer&com=yuio&search=keyword&sort=alpha
Is this possible? I could really use some help here... Thanks! :)
Depending on what language/framework you're using, it may be simpler to put the rewriting/dispatch in the script rather than attempt to do everything in mod_rewrite.
For example, if you were using PHP, given the URL:
http://www.example.com/asdf.php/jkl/id/5/page/2
a script at asf.php could read the PATH_INFO variable, split it on slashes, and write the values into the expected place in $_REQUEST.
Then all you need is one simple rewrite rule to elide the ‘.php’.
You could use a recursive rule:
RewriteRule ^([a-zA-Z]+)/([a-zA-Z]+)/?$ index.php?mod=$1&com$2 [L,QSA]
RewriteRule ^([^/]+/[^/]+)/([^/]+)/([^/]+)/?(.*) $1/$4?$2=$3 [QSA]
But it sure would be easier to parse the request path with PHP.