Add on domain mod rewrite to create pretty url - apache

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

Related

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.

Using .htaccess to make subdirectory act like subdomain

It's a little difficult for me to ask the question correctly in title, but mainly here's what I'm trying to figure out.
If I use .htaccess on my website and say I'm trying to create a specific path "redirect" so that anything inside that path uses it's non-redirected path for it's relative path. Sorry for the structure of that sentence.
So for example...
I have my website as-
http://www.example.com/
I want to keep things organized so I create a sub-folder/subdirectory to keep all these folders inside of.
http://www.example.com/projects/
So the folder projects will be the place holder for all future projects and I want to create a folder inside projects for each project, so that I can define a URL for each one like such...
http://www.example.com/SuperFish/
http://www.example.com/AquaFear/
Now the folder for SuperFish and AquaFear would be under http://www.example.com/projects/ but also in their own folder say...
http://www.example.com/projects/01/
http://www.example.com/projects/02/
Now it'd be fine and dandy to keep the links like such, but for memory purposes and to share with other people the links to those projects, I'd like to just create a nice simpler URL with a custom "name". like http://www.example.com/SuperFish/ instead of http://www.example.com/projects/01/.
Now I was able to do this partially with my .htaccess file in the root folder with this code...
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?$ projects/01/ [NC,L]
RewriteRule ^AquaFear/?$ projects/02/ [NC,L]
The issue with this though is that now I can't use relative paths with my SuperFish or AquaFear made up URL links. Anything such as .css, .js, .etc... Isn't within scope because it's trying to look for those files through the made up URL rather than the real physical one.
So the question is, how would I make it so that I can use my made up URL and also have the webpage load things to the real physical path or somehow fix the relative paths by doing this within the .htaccess file?
The reason I titled this the way it is, is due to subdomains being able to find their files with relative paths even though they're a subdirectory just the same.
Using final $ blocks files inside this two folders from being redirect correctly. You could tried instead :
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^SuperFish/?(.*)$ projects/01/$1 [NC,L]
RewriteRule ^AquaFear/?(.*)$ projects/02/$1 [NC,L]

Changing site URLs in Joomla with Apache mod rewrite

So I'm trying to clean up the URLs on my site, and have digged around for information here and not figured out why I'm still getting errors.
Basically, I have a site that revolves around a search engine, and once the user sees the results and clicks on one, it goes to a URL that looks like:
www.mysearch.com/searchresults/204982398sjfdkf&thisismorejunk=junkjunk=1331
Well, sort of, but you get the point.
I want to clean this up for each result, so it looks like
www.mysearch.com/searchresult1001
I'm using the Joomla platform on my backend, and enabled 'Search Friendly URLs' and there was no problem (although it did almost nothing for me). Then before I enabled 'Use APACHE mod rewrite' I put the following code into my .htaccess file
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^Joomla161_2/joomla\.html http://www.joomla.org/? [R=301,L]
(The last rule was just to see if the rewrite rule works, which it does)
First problem - My host automatically overrides the Options command, saying I can't do it for security reasons - but I figure maybe this isn't big because the rewrite rule still works.
But then when I try to enable 'Use Apache rewrite' my whole site breaks. Worse yet, I have no idea what to do next to actually CHANGE the URLs of my search results.
Options +FollowSymLinks
It is very possible that your host has this enabled by default and
adding to your .htaccess file is causing a conflict.
If your mod-rewrites are working without it then there is no reason
to add it.
Joomla Apache Rewrite
Change the seo settings and htaccess back to default setting.
BEFORE! you change any of the options in the Joomla admin for seo friendly url's or Apache rewrite you must FIRST! rename your htaccess.txt to .htaccess
In some cases you may have to give the server a min or two to notice it.
Now change the Joomla admin settings to seo friendly and enable rewrite.
Search Rewrite
If the above setting are configured correctly then your websites search results will have landing pages with nice urls.

Apache URL Redirect Alternatives

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.

htaccess trick to load resources (js/css) below the web root

I am trying to achieve something that I am not sure if htaccess can even do.
What I have is a folder structure:
myapp
some_folder
some.js
www
css
js
now the web root is pointing at www folder so i can do http://mydomain.com/js/jquery.js and it would work but what I want to do is http://mydomain.com/myapp/some_folder/some.js and it should load some.js and serve it.
Is it possible to do this in htaccess?
AFAIK, you can't point to directories outside the current web root from within .htaccess.
There is the Alias directive but in the .htaccess file, it works only below the web root. It would have to be in the central configuration file to accept absolute paths.
As far as I know, this limitation applies to all other directives that could help here, too (RewriteRule etc.)
The only workaround that comes to mind is using symlinks. Whether you can do that, heavily depends on your operating system and access to the server.
you can redirect upwards, just using the .. parent directory identifier.
for example:
RewriteRule (.*) ../$1
but only if the destination still lies within your webroot.
in your cas it seems not to, so your only chance is to create a symlink/hardlink to that directory or do some file including
alias rewriting in your server cfg file however would work.