.htaccess to partially redirect index requests - .htaccess

I'm doing some work for a friend on their website, and while we are working on the new site I would like to set up their .htaccess file to redirect users with the following rules:
Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
This is probably a simple request, but I have no experience with the rules language that .htaccess uses.
Thanks in advance!!

Redirect any requests to / or /index.html to a subdirectory (e.g. both http://example.com/ and http://example.com/index.html should redirect to http://example.com/legacy/index.html)
RewriteEngine On
RewriteRule ^(index\.html)?$ /legacy/index.html [L]
Allow direct requests to index.php to pass through without redirect (e.g. http://example.com/index.php)
RewriteRule ^index\.php$ - [L]
If possible, requests to a second subdirectory get redirected to index.php (e.g. http://example.com/beta/ redirects to http://example.com/index.php)
RewriteRule ^beta/?$ /index.php [L]
These would all go in the htaccess file in your document root. If you actually wanted to redirect the browser so that the URL in the address bar changes, include a R=301 in the square brackets: [L,R=301]. If when you say "requests to a second subdirectory get redirected to index.php" meaning "any subdirectory", then the rule needs to be changed to:
RewriteRule ^([^/]+)/?$ /index.php [L]

i kind of did the reverse with codeigniter ...
RewriteEngine On
RewriteBase /myproject
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] = 'http://localhost/myproject/';
$config['index_page'] = '';
hope this helps

Related

301 Redirect all pages to root, not index.html, and also redirect all non-www to www

I've been looking for a precise answer to this question for awhile but couldn't find it.
I've launched a one page website in place of an old website with many pages. Now, I want to redirect everything to www.domain.com. The page uses index.html as the homepage, but I don't want to redirect to that, I just want to redirect to the www.domain.com root.
I tried using:
RewriteRule ^.+$ / [R=302,NC,L]
But that just broke my stylesheet and didn't redirect anything. Other solutions I've seen have redirected to the index.html but I want to redirect to the / root domain.
Also, I want to be sure to redirect all non-www pages to www pages too. Can someone please help me out?
Much appreciated
You have most likely something like this in your httpd.conf:
<IfModule dir_module>
DirectoryIndex index.php index.php3 index.html index.htm
</IfModule>
If you request a folder (and http://example.com/ is a request to a folder), it will try index.php first, then index.php3, etc, etc, and the first one that exists it will shown. You'll have to delete that from your httpd.conf if you want every request to end up as http://example.com. It'll show a directory view of your www-root folder unless that has been disabled.
Try:
# Any direct request for html/php pages
RewriteCond %{THE_REQUEST} /[^\?\ ]+\.(html?|php.) [NC]
RewriteRule ^ / [L,R=301]
That won't affect images, or style sheets, etc. Note that this matches against the %{THE_REQUEST} variable because internally the %{REQUEST_URI} gets converted to /index.html so you can't match against that.
If you want non-existent requests (which would normally result in a 404) to be redirected as well:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ / [L,R=301]
To force a "www" use:
RewiteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]

apache .htaccess redirect - clean url

I am working on a php redirect script which 302 redirects visitors to other sites when they access a redirect url..
The script gets a variable (id) from the url and then redirects the visitor to the specific page.
The url structure is : example.com/redirect/index.php?id=test
At the moment all redirects work if I use "ugly" urls, but I want to strip all unnessecary information out of the url with .htaccess rewrites for better usability.
Which .htaccess rewrite rules do I need to make the above shown urls look like : example.com/redirect/test
I am currently using the following .htaccess rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteRule (.*) ./index.php?id=$1 [L]
</IfModule>
but they only work for urls like example.com/redirect/index.php?id=test if I try example.com/redirect/test I get a 404 error page.
It might be good to know, that I have 2 .htaccess files, one in my root directory and one in the root/redirects/ directory.
Best regards !
Try this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)?/?([^/]+)?/?([^/]+)?/? [NC]

Change a url via htaccess

I had a website which url is http://www.testingmyweb.comli.com
In my website a had a folder TCWEB. My website is in this folder.
I want to map http://testingmyweb.comli.com/TCWEB/home.html to http://testingmyweb.comli.com/
What can I changes do in .htaccess file?
Based on your comment, your htaccess file looks like this:
RewriteBase /
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Redirect /TCWEB/home.html testingmyweb.comli.com
The rule that you have appends .html to requests that are for html files (but don't have the extension). The redirect that you have underneath that needs to be removed. In order for you to rewrite / to /TCWEB/home.html, that mod_alias directive will cause a loop. Replace it with these rules:
RewriteRule ^$ /TCWEB/home.html [L]
RewriteCond %{THE_REQUEST} /TCWEB/home.html
RewriteRule ^ / [L,R=301]
The first rule makes it so requests for / maps to /TCWEB/home.html, but the URL in the browser's address bar remains http://www.testingmyweb.comli.com. The second rule does what the Redirect does, except it checks that the request was actually made for /TCWEB/home.html, and not an internally rewritten URI. So when someone goes directly to /TCWEB/home.html, they get redirected to /, then the first rule gets applied.

Taking a site down temporarily with Codeigniter (301 possible with routes.php?)

I'm using Codeigniter with the standard .htaccess rewrite rules so that no /index.php/ is visible within the urls.
Now I recently needed to take the site down temporarily and therefore wanted to redirect everyone to a 'down' page. The following worked:
$route['default_controller'] = "down";
$route['(:any)'] = "down";
But I'm aware that in this case, a 301 is really appropriate.
How and where should I set that? I don't see a way to specify it in the routes.php and was confused about how to do it within the .htaccess because of the existing rules....
RewriteBase /
RewriteCond %{REQUEST_URI} ^_system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^myapp.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond $1 !^(index\.php|resources|files|robots\.txt)
RewriteRule ^(.*)$ index.php/$1
You don't want a 301 redirect. 301 means "Permanent", and this should only be a temporary redirect. And you can do this with htaccess if you add this above all your other rules:
RewriteRule ^ /down.html [L,R=302]
As long as it's above any of the other rules, then any request will get redirected to /down.html. If you don't want to externally redirect the browser (e.g. have the /down.html URL show up in the URL address bar), then remove the ,R=302 bit from the square brackets and the URL address bar would stay unchanged while the content served is from down.html.

htaccess Redirect 301 problem .. all redirects with one string fail to redirect and 404

So I have moved a website and am trying to 301 redirect everything, which I do quite often so this is a weird problem but probably something stupid I'm not seeing.
ALL of my redirects are working fine, except any redirect that the first string starts with "/Dining" or "/dining" are failing. For example, this redirect works fine-
Redirect 301 /healthfitness/teeth.cfm /healthcare/pretty-teeth
...as well as 100s of others.
But all of these are failing (many more than I'm showing)-
Redirect 301 /Dining/diningreviews/vawines.cfm /shopping/wines-2004
Redirect 301 /Dining/diningathome/carrotcake.cfm /home-garden/carrot-cake-2003
Redirect 301 /Dining/diningathome/oldvarolls.cfm /home-garden/virginia-rolls-2003
Redirect 301 /Dining/diningathome/pumpkincake.cfm /home-garden/pumpkin-cake-2003
The top of my .htaccess file looks like this-
RewriteEngine On
RewriteBase /
#uploaded files
RewriteRule ^(.*/)?files/$ index.php [L]
RewriteCond %{REQUEST_URI} !.*wp-content/plugins.*
RewriteRule ^(.*/)?files/(.*) wp-content/blogs.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteCond %{REQUEST_URI} ^.*/wp-admin$
RewriteRule ^(.+)$ $1/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule . - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-.*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
<IfModule mod_security.c>
<Files async-upload.php>
SecFilterEngine Off
SecFilterScanPOST Off
</Files>
</IfModule>
#Everything below here are Redirect 301s
Dont redirect statements have to include the protocol in the destination?
You must include the domain name in the redirect.
Never mix mod_alias and mod_rewrite directives in the same file. If you use RewriteRule for any of your rules, you must use RewriteRule (not Redirect or RedirectMatch) for ALL of your rules.
List all redirects (using RewriteRule syntax) before any of the rewrites (using RewriteRule syntax) otherwise you will inadvertently expose rewritten pointers back out on the the web as new URLs.
The code you are currently using is very very inefficient. The .* patterns mean your .htaccess file will attempt hundreds of thousands of "back off and retry" trial matches for every URL request hitting the server.
In particular,
^(.\*/)? should be replaced by ^([^/]+/)\* in two places.
^.\*/ should be replaced by ^/([^/]+/)\*
!.\*wp-content/plugins.\* should be replaced by !wp-content/plugins
^([_0-9a-zA-Z-]+/)?(.\*\\.php)$ should be replaced by ^([_0-9A-Z-]+/)?([^.]+\\.php)$ with the [NC] flag also added.
The "add a trailing slash to /wp-admin" redirect should be the very first ruleset in the file and the target URL should include the protocol and domain name. That rule should be immediately followed with a non-www to www canonicalisation rule.
The new code may well run hundreds of times quicker.

Resources