Apache URL Redirect Alternatives - apache

One of my clients (before I came along) decided to use htaccess redirects as their form of URL shortening/search engine friendly URLs. They have literally thousands of them.
The new version of the site now has friendly urls but they aren't equivalent to their redirects so they still need them.
My question to you all is: Is there another way than to populate this file with thousands of lines of "Redirects /folder1 /folder2"?
Thanks

If you cannot make simple rules to catch all of them as in the #chris henry solution you can use the RewriteMap utility of mod_rewrite. You'll be able to write these thousand rules in a text file, then make this text file an hash file, and mode_rewrite will try to match url in this file (if it's an hash file it's quite fast). After that mode_rewwrite can generate a redirect 301 with the [L,R=301] tag.

Yep, look at using the Apache config (httpd.conf or httpd-vhosts.conf) to set up site wide folder aliasing. Eg:
Alias /folder1 c:/www/folder2
Look at http://httpd.apache.org/docs/2.0/mod/core.html#directory for more info.

Depending on how different the URLs being redirected are, one solution might be to come up with an rewrite rule that covers all of them, and maintain the short / long URLs in your application, or even a database.

Related

How to do a basic, total, overall redirect with htaccess

For some time I used a free account to host my online forum. Recently I bought a domain name, and now it contains a full copy of all the php and mysql data in the forum, so now there are two copies of the forum in different locations. The last step I need to accomplish is to erase all the data in the old location, and replace it with a short htaccess file that will make a global redirect. What would the contents of this htaccess file look like ? I don't know enough Apache to be sure.
I found many related topics to this one here on MSE and elsewhere, but none that could match my needs exactly . Most are about redirections to just a subfolder inside a single server.
Note : the whole point of this question is that I wish the users of the forum to use the same URL as before, I wish to avoid to have to tell everyone that the forum's URL has changed. Nonprofessional users dislike changes
If mod_alias is active:
Redirect 301 / http://your-new-site.com
otherwise, if mod_rewrite is active:
RewriteEngine On
RewriteRule ^ http://your-new-site.com%{REQUEST_URI} [R=301,L]

How to implement url page redirection for a massive huge website

my site e.g. carparts.co.uk has 355000 unique urls. (it is a car parts catalogue site) (on webmaster tools it shows that 174000 of these are indexed)
We want to move our site to a new shopping cart platform (prestashop), and have completely changed the structure of the catalogue, which means we now have a new set of urls. (although the main domain is unchanged and is still carparts.co.uk)
i now have a excel sheet where I have a column of the 355000 'old' urls matched against the closest equivalent url on the new catalogue.
e.g.
old url: "carparts.co.uk/ford-ranger-alternator belts.htm"
goes to: "carparts.co.uk/belt-drive"
(and there are 355,000 of similar redirects)
my question is how should i do this?
i've that you can use htaccess to do this, but i'm worried because i've read that htaccess slows down sites if it is very large (is this slowness only encounted when trying to access one of the old urls?, or will it impact the speed of all my urls?
so what is the best thing for me to do with such a large number of urls?
Your best bet is probably setting up a RewriteMap. This requires server vhost config access as you can't configure the map from an htaccess file (though you can use one). The mapping is cached by apache so you don't need to worry about constant file access.
Something simple like:
RewriteMap redirects txt:/full/path/to/redirect-map.txt
Then in the file redirect-map.txt would simply have a "from" and "to":
"ford-ranger-alternator belts.htm" belt-drive
old-url.htm new-url
etc...
Then in either your htaccess file or in vhost config, just do:
RewriteCond $(redirects:$1|0) !=0
RewriteRule ^(.*)$ $(redirects:$1) [L,R]
Use of htaccess slows down the website because it needs to check several files for each request, and these are checked dynamically for every request.
It's more a problem for deep routed sites. For example, a request to:
www.example.com/folder1/folder2/folder3/folder4/index.htm
would need to check
The main config file.
Then add any overrides in the document root
htaccess file.
Then add any overrides in the folder1 htaccess file.
Then add any overrides in the folder2 htaccess file.
...etc.
However if you don't have deep nesting then it's not so bad. Still slower than not using them, but may not be noticeable on most sites.
The benefit of htaccess for you here, would mean that you wouldn't need to put all the redirects in one place, and could split them up amongst the htaccess file. I'm not sure of the impact of adding 355,000 redirects to the main Apache config, but it is a fair number, so imagine it could have a performance impact. The htaccess files, on the other hand, are read dynamically as the request is made, so all the redirects would not need to be loaded into Apache.
So, this might be one of the few use cases where htaccess might be a better solution, even if you do have access to the main config files.

Handling several thousand redirects with .htaccess

I am working on a site overhaul. As a result I am moving several pages over to a new format. They aren't keeping the same file name as before so the migration is a little tricky.
Example:
news.alpinezone.com/93467/ is becoming
http://alpinezone.com/still-more-skiing-and-riding-at-whiteface/
The news subdomain has accumulated in several years over 3000 articles. Is it OK to put 3000 + 301 redirects into an .htaccess file?
On a side note, for proper SEO, should I also make sure I use http:// instead of http:// www and also make sure they are fully lower case and also close with a / at the end of the URL. I am redesigning into wordpress and any combination pretty much works but I understand that for Google they can be considered unique but similar URL's so I want to stick with one as much as possible.
Thanks!
Apache does have some stuff for this, like RewriteMap or RewriteProg. I think htaccess files are read on every request, so I wouldn't want to make the size of it explode with 3000 lines of text - although I gut tells me it would handle it just fine. I think RewriteMap is only loaded once per server start or somethign like that, so thats a benefit.
But personally, I think I would just do an internal rewrite of any request to the news subdomain to a serverside script like php, and then inspect the uri, query the database to get the most current/up to date url slug for the id, and then do an external 301 redirect to the new url.
Have you considered mod_authnz_ldap to offload the authentication and authorization lookups to another server? I use this particular module on several enterprise servers with no problems whatsoever. It easily allows you to set up access to pages by group etc.

How do I rewrite URLs with Nginx admin / Apache / Wordpress

I have the following URL format:
www.example.com/members/admin/projects/?projectid=41
And I would like to rewrite them to the following format:
www.example.com/avits/projectname/
Project names do not have to be unique when a user creates them therefore I will be checking for an existing name and appending an integer to the end of the project name if a project of the same name already exists. e.g. example.project, example.project1, example.project2 etc.
I am happy setting up the GET request to query the database by project name however I am having huge problems setting up these pretty url's.
I am using Apache with Nginx Admin installed which mens that all static content is served via Nginx without the overhead of apache.
I am totally confused as to whether I should be employing an nginx rewrite rule in my nginx.conf file or standard rewrites in my .htaccess file.
To confuse matters further although this is a rather large custom appliction it is build on top of a wordpress backbone for easy blogging functionality meaning that I also have the built in wordpress rewrite module at my disposal.
I have tried all three methods with absolutely no success. I have read a lot on the matter but simply cannot seem to get anything to work. I am certain this is purely down to a complete lack of understanding on with regards to URL rewriting. Combined with the fact that I don't know which type of rewriting should be applicable in my case means that I am doing nothing more than going round in circles.
Can anyone clear up this matter for me and explain how to rewrite my URLs in the manner described above?
Many thanks.
If you are proxying all the non static file requests to Apache, do the rewrites there - you don't need to do anything on nginx as it will just pass the requests to the back end.
The problem with what you are proposing is that it's not actually a rewrite, a rewrite is taking the first URL and just changing it around or moving the user to another location.
What you need actually takes logic to extrapolate the project name from the project ID.
For example you can rewrite:
www.example.com/members/admin/projects/?projectid=41
To:
www.example.com/avits/41/
Fairly easily, but can you map that /41/ in your app code to change it to /projectname/ - because a URL rewrite can't do that.

Add on domain mod rewrite to create pretty url

I have a main website www.site.co.uk and one of my add on domains is addon.co.uk. Site has an htaccess as does addon. The folder of which from the root would be www.site.co.uk/addon.co.uk/.htaccess ..I think!
Anyway currently I can do redirects within addon htaccess file fine, but its a database driven site and im trying to create pretty urls for it, so:
http://www.addon.co.uk/addonsites/some.php?id=page
would become:
http://www.addon.co.uk/id/page/
The mod I have in the addon htaccess file is the following:
RewriteEngine On
RewriteRule ^id/([^/]*)/$ /addonsites/some.php?id=$1 [L]
But this has no effect.
Well, the mod_rewrite module will perform translations on requests to the server, so when anyone requests the resource located at http://www.addon.co.uk/id/page/ the server will know that http://www.addon.co.uk/addonsites/some.php?id=page is the place to go.
However, mod_rewrite does in no way modify your existing links. I.e., you should rewrite the HTML (or scripts generating the HTML) to match the "new" way of linking. E.g., if you have ... somewhere on your site, you must make sure it is changed to ....
tl;dr
mod_rewrite handles incoming requests; it does not modify your output (HTML).