Too many Rewrite Rules in .htaccess - .htaccess

I had to redesign a site last week. The problem is that last urls weren't seo friendly so, in order to avoid Google penalizing my site because too many 404 errors, I have to create a lot of Rewrite Rules because all the content had awful URL's ( and that content had a good position on SERP's).
For example:
RewriteRule ^documents/documents_for_subject/22-ecuaciones-exponenciales-y-logaritmicas http://%{HTTP_HOST}/1o-bachillerato/matematicas-cc.ss/aritmetica-y-algebra/ecuaciones-exponenciales-y-logaritmicas [R=301,L]
Is this a problem on my performance? Is there another solution to my situation?
Thanks

They are in the same domain.
Then an internal redirect is much better. A header redirect sends the new URL to the browser and causes it to make a new request; an internal one is handled, as the name says, internally.
This should work:
RewriteRule ^documents/documents_for_subject/22-ecuaciones-exponenciales-y-logaritmicas /1o-bachillerato/matematicas-cc.ss/aritmetica-y-algebra/ecuaciones-exponenciales-y-logaritmicas [L]
Any performance issues are going to be negligible with this - except maybe if you have many thousands or tens of thousands of individual rules, those may slow down Apache. In that case, if you have access to the central server configuration, put the rules there instead of a .htaccess file, because instructions in the server config get stored in memory and are faster.

A. Yes using 301 is the right way to notify search bots about changed URLs and eventually your old URL's will be removed from search results.
B. You don't need to use %{HTTP_HOST} in your rewrite rule just use it like this:
RewriteRule ^documents/documents_for_subject/22-ecuaciones-exponenciales-y-logaritmicas http://%{HTTP_HOST}/1o-bachillerato/matematicas-cc.ss/aritmetica-y-algebra/ecuaciones-exponenciales-y-logaritmicas [R=301,L]
C. If you have lots of RewriteRules like above I recommend using RewriteMap or else use some scripting support (like PHP) to redirect from old to new URL with 301.

Related

How to redirect a URL in htacess with query string to the same URL but different query string

Because of removing several languages from a multi-language website, I need to 301 redirect pages that end with ?lang=da, ?lang=de, and ?lang=nl to the same URLs but ending in ?lang=en. It sounds like a common scenario, but I haven't found the right code yet to accomplish this or tried some code because the purpose of that code was either to redirect to one new URL or to replace the URL but not the query string, while I need to replace the query string and keep the URL.
This probably is what you are looking for:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^lang=(da|de|nl)$
RewriteRule ^ %{REQUEST_URI}?lang=en [QSD,R=301,END]
For this to work the rewriting module obviously needs to be loaded into your http server and it has to be activated for your http host too.
It is a good idea to start out with a 302 redirection and to only change that into a 301 once you are convinced everything is set up as required. That way you prevent ugly caching effects...
You can implement such rules in the http server's host configuration. Or, if you do not have access to that, you can use a distributed configuration file (".htaccess"), but that has performance disadvantages and you also need to enable the interpretation of such files first. Please see the documentation of the tool you are using to learn how to do that.

Generating friendly URLs / htaccess from database

The scenario
I have a menu builder (Title, Link...) in PHP/MySQL, which I want to be able to configure the friendly SEO rule in the .htaccess
The issues
Here is a typical rewrite rule at the moment:
RewriteRule ^computer/other-components/?(.*)$ index.php?CategoryID=9&SubCategoryID=2 [NC,L]
I could write all these manually in the .htaccess but there are a couple of issues:
I would have to manually do this each time I want to add/make changes to the page structure
It's really long.
I've read that writing directly to the .htaccess is a no-no for security, so what would be best to implement something like this? Wordpress and similar CMSs do similar, so what is recommended here?

301 Redirect vs Rewrite

I have a site that was hosted by someone else all the web pages were .html files. I am now hosting the site and have changed it to a wordpress site. The domain has not changed but obviously all the pages have. What is the best way to redirect all the .html pages to the main url?
301 Redirect in an .htaccess does not require the mod_rewrite library. It's a much simpler way to redirect, but it doesn't have the flexibility and power you get using the Rewrite rules. If you have a 1-1 mapping with explicit urls you can use the Redirect:
Redirect 301 /path/file.html http://new.site.com/newpath.php
If you're trying to do wild card matching of a number of similar patterns using regular expressions you'll need to use Rewrite.
RewriteRule ^(.*).html$ http://new.site.com/$1.php [R=301,NC,L]
Here's a pretty good overview of the 2 methods: http://www.ksl-consulting.co.uk/301-redirect-examples.html
There is also RedirectMatch that also does wild card matching of similar patterns using regular expressions. The choice depends on just what you need to do.
Rewrite is complex - learning curve - but you can serve alternative urls without giving a HTML code and things that seem imposible. But with great power comes complexity and lots of bugs.
If you are only doing a simple redirection - possibly matching some urls - redirect is the way to go.
When you can't do it with Redirect, you will probably want to start learning Mod_Rewrite.

Can you ReWrite a htaccess ReWriteRule?

Just wondering if its possible to 301 redirect an existing Rewriterule?
For example if I have the following line in my .htaccess file :
RewriteRule ^blue-widgets/ bluewidgets.php
and then I need to change my URL structure but the url "blue-widgets/" has a good ranking in the search engines which I dont wont to lose, is it possible to add another rewrite rule (301) that redirects that url too "newdirectory/blue-widgets/" ? If so, how is this done, is it a simple case of adding the new rewriterule under the existing one?
Does the fact that you have 2 rewrites, slow the page down or have any other problems?
You are confusing two quite different aspects: internal and external rewrites.
301 and 302 are external rewrites and in effect pass the redirect instruction back to the user's browser to do. 301 tells the browser (and the search engines) that the address change is permanent.
Rewrite rules without the [R] flag do an internal redirect -- that is a remapping inside the Apache / IIS subsystem than is not exposed to the outside world.
Yes, you can have multiple URI internally redirecting to the same target, but as you've written them, they will not be external and not 301s.
Try
RewriteRule ^blue-widgets/$ /new-directory/blue-widgets/ [L,NC,R=301]
RewriteRule ^new-directory/blue-widgets/$ bluewidgets.php [L,NC]
Does the fact that you have 2 rewrites, slow the page down or have any other problems?
The 301 to send blue-widgets to new-directory/blue-widgets is cached and will only happen once per client, so the performance should be minimally affected.
However, if you can, you should also change this link on your site to be new-directory/blue-widgets

URL/Subdomain rewrites (htaccess)

Say I have the following file:
http://www.example.com/images/folder/image.jpg
I want to serve it on
http://s1.example.com/folder/image.jpg
How can I do a htaccess rewrite to point it to it?
Like for example, I make a subdomain s1.example.com and then on that subdomain, I add a htaccess rule to point any files, to pull it from http://www.example.com/images/
Does serving files this way act as serving content from a cookieless domain?
First let me talk a bit about the concept of cookieless domains. Normally, when requesting anything over http, any relevant cookies are sent with the request. Cookies, are dependent on which domain they come from. The idea of using a cookieless domain is that you relocate static content that doesn't cookies, like images, to a separate domain so that no cookies will be sent with that request. This cuts out a small amount of traffic.
How much you gain from doing this depends on the type of page. The more images you have, the more you gain. If your site loads a big bunch of small images, such as avatars or image thumbnails, you might have a lot to gain. On the contrary, if your site doesn't use any cookies, you have nothing to gain. It's entirely possible that your page won't load noticeably faster, if it only uses a small amount of images, which will be cached between page loads anyway.
One thing to keep in mind, too, is that cookies set for example.com will also be sent with requests to s1.example.com as "s1." is a subdomain to example.com. You need to use www. (or any other subdomain of your choice) in order to separate the cookie spaces.
Secondly, if you decide that a cookieless domain is actually something worth trying, let's talk about the implementation.
Shikhar's solution is bad! While the solution appears to work on the surface, it actually defeats the purpose of using a cookieless domain. For every image, first the s1. url is tried. The s1. URL then makes a redirect to the www. domain which triggers a second http request. This is a loss, no matter how you look at it. What you need is a rewrite, which changes the URL internally on the web server, without the browser even realizing.
For simplicity, I'm assuming that all domains point to the same directory, so that www.example.com/something = example.com/something = s1.example.com/something = blub.example.com/something. This makes things simpler if you really need store the images physically in "www.example.com/images".
I'd recommend a .htaccess that looks a little something like this:
# Turn on rewrites
RewriteEngine On
# Rewrite all requests for images from s1, so they are fetched from the right place
RewriteCond %{HTTP_HOST} ^s1\.example\.com
# Prevent an endless loop from ever happening
RewriteCond %{REQUEST_URI} !^/images
RewriteRule (.+) /images/$1 [L]
# Redirect http://s1.example.com/ to the main page (in case a user tries it)
RewriteCond %{HTTP_HOST} ^s1\.example\.com
RewriteRule ^$ http://www.example.com/ [R=301,L]
# Redirect all requests with other subdomains, or without a subdomain to www.
# Eg, blub.example.com/something -> www.example.com/something
# example.com/something -> www.example.com/something
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} !^s1\.example\.com
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
# Place any additional rewrites below.
Just for people's general info who like me may be investigating the benefits of this. From what I'm reading it isn't just cutting down on the upstream overhead of eliminating cookies sent with http requests. Apparently many browsers limit max connections to 1 domain/server to 6 concurrent. So if you have a separate domain on a diff server you get to double that to 12. Which to me would seem like the main potential here for a serious speed boost.
Though anyway, if I'm understanding this correctly. The other domain serving the static content needs to be located on another server from the main domain. Actually makes sense, avid firefox user and tweaker. When you check the about:config settings in firefox the max connections per server is set to 6 by default. A person can manually bump it up to a max of 8. But most firefox users probably don't spend enough time getting familiar with how to modify the browser and leave it to the default max of 6.
Not sure how many the other browsers set by default and then there is older browser versions that are still in use to consider. Bottomline ... makes perfect sense that enabling the browser to double the total number of connections using two servers would have to be a loadtime improvement. Using a sub-domain on the same server a person isn't going to be able to take advantage of that.
If you mean to redirect the traffic from www.example.com to s1.example.com, use the following htaccess on www.example.com
RewriteCond %{HTTP_HOST} ^(s1\.example\.com)
RewriteRule (.*) http://www.example.com%{REQUEST_URI}[R=301,NC,L]
If this is not what you are looking for, elaborate the question further.
I think you may have it backwards, (or very possibly I do). To clarify, if you're implementing a cookie-less subdomain & have a base URL of www. at least in this case, cookies are set on www, for example: a major cookie setter is google analytics, so when setting their script on my site it looks like this:
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'analytics-acc-#],
['_setDomainName', '[www.valpocreative.com][1]'],
['_trackPageview']);
You can see here that I set my main domain to www, correct me if i'm wrong in my case I would need to redirect www to non www subdomain & not the other way around. This is also the cname setup made on my cpanel (cname= "cdn" pointing to www.domain.com)

Resources