Can one include separate files in htaccess for a list of redirects? - .htaccess

My main htaccess file does a bunch of things for my site to function correctly.
I have added redirects for pages that have moved. I don't have root access to the server and using .htaccess is my only option.
Is it possible to include separate files for the redirects in the .htaccess file so I can keep them separate and write programatically to the additional files that hold my redirects?
Basically I want to reference separate files from my .htaccess to manage rules dynamically and also neaten up one long .htaccess file with a few smaller files.
I also want to add redirect rules on the fly as things change on the site within my application.

You can use a RewriteMap http://httpd.apache.org/docs/2.3/rewrite/rewritemap.html
Let's say your map file looks like this and is called moved.map:-
/about profile
/page/that/has/moved new/location
You .htaccess would need something like this:-
RewriteMap moved txt:moved.map
RewriteCond %{REQUEST_URI} ^(.*)$
RewriteCond ${moved:%1|NOT_PRESENT} !NOT_PRESENT [NC]
RewriteRule .? ${moved:%1} [NC,R=301]
This will redirect with a 301 status code http://your.domain.com/about to http://your.domain.com/profile and redirect http://your.domain.com/page/that/has/moved to http://your.domain.com/new/location
You can then programmatically create moved.map.
I hope that helps.

If you are using .htaccess files then don't bother with RewriteMap -- it only applies if you have root access to the server or vhost config, which is never the case when you purchase a shared service offering.
If you are constrained to use .htaccess files then you have two options:
The first is to do what some packages do and that is to get your application to rewrite the .htaccess file based on a rewrite map that you maintain within in it. The best way to do this is to have "bookends" in your .htaccess file e.g.
##++AUTOMATIC rewrite rules
<rules inserted by your app>
##--AUTOMATIC rewrite rules
And when an update occurs have your app read in the .htaccess, swap out the section between ##(++|--)AUTOMATIC rewrite rules, write it back to a temp file, then move the temp file to .htaccess (this makes the rewrtie-back atomic on *nix OSs).
The second which might work if you know some regexp regular pattern which covers the rewrites (this is often the case) then use a rule to map them to a redirector script which looks up the new target and itself issues a:
$server = $_SERVER['HTTP_HOST'];
header( "Location: http://$server/$newTarget?$parameters", TRUE, 301 );
Note the 301 redirect -- this means that client browsers should cache this and remember this in future.

Related

Rewrite URL with .htaccess for 2 parameters

How can I convert URL from:
http://example.com/site/delete-page.php?site=first&page=second
To:
http://example.com/site/first/delete-page/second
Thank You!
This is the normal rule for that specific task:
RewriteEngine on
RewriteRule ^/site/([^/]+)/delete-page/(.+)$ /site/delete-page.php?site=$1&page=$2 [L]
To use the same rule inside a .htaccess style file you have to modify it slightly: remove the leading slash (/) from pattern and target:
RewriteEngine on
RewriteRule ^site/([^/]+)/delete-page/(.+)$ site/delete-page.php?site=$1&page=$2 [L]
This assumes that the .htaccess style file is located inside the folder holding the site folder. You can also place the file inside that site folder, then obviously you have to remove the site/ part from the rules pattern and target. Also you have to take care that the interpretation of .htaccess style files is enabled at all for that host and location.
In general you should always prefer the first version and place such rules inside your http servers host configuration. .htaccess style files are notoriously error prone, hard to debug and they really slow the server down. They are only offered as a last option for situations where you do not have access to the server configuration. For example when using a really cheap shared web hoster service...

Clean URL rewriting rule

right now my url looks like this:
http://domain.com/en/c/product%2C-product2%2C-product3/82
where last number is category numer.
And im trying to rewrite it and redirect user to url which should look this one:
http://domain.com/82/product-product2-product3
The clue is I want to hide "en/c/" part and clean url from commas and blank spaces. I'm completely green in rewriting.
Any ideas?
You can use these 2 rules in your root .htaccess for that:
RewriteEngine On
RewriteBase /
RewriteRule ^en/c/([^,\s]*)[,\s]+([^,\s]*)/(\d+)/?$ $3/$1$2 [NC,L,NE,R=302]
RewriteRule ^(en/c)/([^,\s]*)[,\s]+(.*)/(\d+)/?$ $1/$2$3/$4 [NC,L]
In order for this to work, we need to tell the server to internally redirect all requests for the URL "url1" to "url2.php". We want this to happen internally, because we don't want the URL in the browser's address bar to change.
To accomplish this, we need to first create a text document called ".htaccess" to contain our rules. It must be named exactly that (not ".htaccess.txt" or "rules.htaccess"). This would be placed in the root directory of the server (the same folder as "url2.php" in our example). There may already be an .htaccess file there, in which case we should edit that rather than overwrite it.
The .htaccess file is a configuration file for the server. If there are errors in the file, the server will display an error message (usually with an error code of "500").
If you are transferring the file to the server using FTP, you must make sure it is transferred using the ASCII mode, rather than BINARY. We use this file to perform 2 simple tasks in this instance - first, to tell Apache to turn on the rewrite engine, and second, to tell apache what rewriting rule we want it to use. We need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^url1/?$ url2.php [NC,L] # Handle requests for "url1"

Multiple 301 Redirect For Multiple Pages or URL

I am redesigning my store and so the old structure has been changed with the new structure. So trying to redirect all old products with the new.
I have more than 20-25 products and if I write redirect rule for every product than I have to write in this way
Redirect 301 /store/products/somename/ http://store.domain.com/nicecar
Redirect 301 /store/products/blabla/ http://store.domain.com/newpros
Redirect 301 /store/products/cubacuba/ http://store.domain.com/illollo
Which will become to long and may slow down the site. So is there anyway to optimize this redirect rule?
Thanks a lot
It is better to use RewriteMap for your requirement. Here is an example how to use it:
Add following line to your httpd.conf file:
RewriteMap prodMap txt://path/to/prodMap.txt
Create a text file as /path/to/prodMap.txt like this:
somename nicecar
blabla newpros
Add these line in your .htaccess file under DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^store/products/([^/]+)/?$ http://store.domain.com/${prodMap:$1} [L,R=301,NC]
If you have access to the vhost or server config, you can setup a rewrite map, though in reality, it's probably marginally faster than just having a ton of redirects. The redirects that you have in your htaccess file are cached, so as long as the htaccess file isn't modified, the directives don't need to be re-read.
There's a very detailed tutorial for how to use RewriteMap and the many various mappings you can use with it.

Multiple htaccess files with different rewrite rules

I have 2 .htaccess files - one in root directory, another in subdirectory:
accordingly docroot/ and /subdirectory/docroot (this one works like a separate account).
The problem is that there are different rewrite rules in both files. Basicly, the problem is that the .htaccess in subdirectory doesn't work and/or is ignored.
What I am trying to achieve is to have one set of rewrite rules for docroot and other set of rewrite rules for subdirectory/docroot
edit:
the .htaccess file in subdirectory/docroot basicly strips index.php from url, and it actually works, but when i go to subdirectory/user it redirects to subdirectory/user/login (instead of subdirectory/index.php/user/login -- this parts is ok) but the website shows the root page (not subdirectory, but domain root)
Any ideas?
I just had to set RewriteBase to that particular subdirectory to make things work.

using htaccess to set default folder instead of file?

Is it possible to set a default folder to access instead of a file like "index.html".
What I'd like to to is make it so that when a person visits my site they get redirected to a folder within the root of the domain. I am using a blogging engine and I need it to show up as the homepage but I don't want to install it in the root because I have other folders and files that need to be in the root directory. And I don't want to put them inside the blogging software's folder. I also don't want to use a 301 or 3XX redirect for SEO purposes.
If there's a way to do what I'm asking let me know. If not, let me know the best option otherwise.
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule ^$ foo/bar [L]
This will rewrite requests to the directory where this rule is applied to to foo/bar. So if you put this rule in the .htaccess file in your document root and request http://example.com/, it will get rewritten to http://example.com/foo/bar.

Resources