I'm looking to do a .htaccess file that let any requests pass to its original destination but redirects to a specific folder (a splash screen in this case) is no destination is specified. I'm not very well-versed with .htaccess and would appreciate some help.
Example: I'm requesting http://www.domain.com/folder/file.php, it should go through. But if I'm requesting http://www.domain.com/, it should redirect to http://www.domain.com/splash/.
What I have so far redirects correctly to /splash/, but redirects everything to /splash/.
<IfModule mod_rewrite.c>
RewriteEngine On
# If the requested URI is empty...
RewriteCond %{REQUEST_URI} !^$
# ...then redirect to the "splash" folder
RewriteRule .* splash [L]
# Otherwise rewrite the base
RewriteBase /
# If the request is not a folder or a file, redirects to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks!
EDIT: One important point would be to redirect http://www.domain.com/ to http://www.domain.com/splash/, but allow direct access to http://www.domain.com/index.php.
The %{REQUEST_URI} variable includes a leading slash, so it will NEVER be blank. You can get rid of that and just use this rule:
RewriteRule ^/?$ /splash/ [L,R]
If you want the URL that appears in the browser's address bar to stay http://www.domain.com/, then remove the ,R from the square brackets: [L].
Related
We have a server, and have several folders under the /var/www/ folder.
We have pointed our domain name to the IP of the server, and by default we expect to load one folder as the website (since its not possible to point a folder with DNS).
I have written .htaccess in such a way that when you enter the IP or the domain name, the request redirects to the website folder.
However, whenever we enter the IP or the domain name, the name of the folder is getting added to the URL.
Here is the present .htaccess:
Options +FollowSymlinks -Multiviews
#DirectoryIndex folder/
RewriteEngine on
ReWriteBase /
RewriteRule ^$ /folder [L]
RewriteRule ^$ folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
where the folder is the folder's website
so,
www.domain.com
becomes
www.domain.com/folder/
Is there a way to rewrite the URL to remove the folder name?
Thanks in advance :)
EDIT : Added .htaccess code
Have your rule like this in DocumentRoot/.htacess:
DirectorySlash On
Options +FollowSymlinks -MultiViews
RewriteEngine on
ReWriteBase /
# redirect /folder/abc123 to /abc123
RewriteCond %{THE_REQUEST} \s/+folder/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# skip rewrites for real files/directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule !^(index\.)?$ - [L]
# route everything internally to /folder
RewriteRule ^((?!folder/).*)$ folder/$1 [L,NC]
It sounds like you made an external redirect instead of an internal rewrite. An external redirect is denoted by the [R] flag (with optional parameter) and causes Apache to send a 301 or 302 header back to the client with a different url that client should request. This causes the client to show the new url in the address bar.
What you want is an internal rewrite. When you request the url, the url is internally rewritten to the place where the resource is actually located. You do this by omitting the [R] flag, and not using a domain name in the rewritten part. It typically looks something like this:
RewriteCond %{REQUEST_URI} !^/folder
RewriteRule ^(.*)$ /folder/$1 [L]
If none of my RewriteRule [L] matches, I want to redirect to a nice url /you/shall/not/pass, but show contents of /index.html.
This is what I am doing now (this is the very last rule in the file):
RewriteCond %{REQUEST_FILENAME} !-f # is not file
RewriteCond %{REQUEST_FILENAME} !-d # is not directory
RewriteCond %{ENV:REDIRECT_STATUS} !=200 # wasn't already redirected
RewriteRule .* index.html
It works fine, but keeps whatever garbage was written in the URL. I want it to be changed.
Doing this didn't work
#RewriteCond same as above
RewriteRule .* /you/shall/not/pass [R]
RewriteRule ^/you/shall/not/pass index.html
I apparentely don't understand how [R] works, whether it continues forwarding the changed url to other RewriteRules or not and what page it redirects to when the end of file is reached.
You can use ErrorDocument 404 with a rewrite rule for this:
Options +FollowSymLinks
ErrorDocument 404 http://domain.com/you/shall/not/pass
Then create a symbolic link like this:
/public_html/you/shall/not/pass -> /public_html/index.html
Replace /public_html/ with your DocumentRoot path.
So, after countless hours I made it work, somehow, I guess...
I am too tired to investigate, but I will get back later and edit if it is wrong.
ErrorDocument 404 http://domain.com/you/shall/not/pass # redirects if document does not exist
RewriteEngine on
RewriteRule ^$ /you/shall/not/pass [L,R] # redirects if request URI is empty, skips rest
RewriteRule ^you/shall/not/pass$ /index.html [L] # puts different resource to matched url
It's kinda weird that the solution is that short.. How come I didn't try that already?
I want to rewrite /home to the index.php and /edit to edit.php, but everything else, like /asdfg I want to redirect to new.php
Both edit and home on their own work, but the (.*) takes priority over those rules, I tried it without the RewriteCond, but then nothing works.
RewriteEngine on
RewriteBase /
RewriteRule ^home$ /index.php
RewriteRule ^edit$ /edit.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /new.php [L,R=301]
I just need all undefined URI's to redirect to /new.php
I also need to keep the /asdfg in the URI, so I can grab it with JavaScript and show the appropriate content, an either show a page to let ppl make a new post or to show existing content.
I can't define all existing content, because that comes from a database, only a few predefined, reserved "/folders" will redirect to predefined pages.
I have the following working:
RewriteEngine on
RewriteRule ^home$ /index.php [R]
RewriteRule ^edit$ /edit.php [R]
It leaves the /home there and go to index.php
Now I just need to redirect all unknown requests to a default page new.php.
I basically want to prevent users getting a 404 or any other error, ever..
You need to add the last flag to your first RewriteRules, so that the last RewriteRule does not take effect:
RewriteRule ^home$ index.php [L]
RewriteRule ^edit$ edit.php [L]
Try adding the [L] flag to the "home" and "edit" rules, so that the "new" rule isn't processed once they're matched.
Some... fine person... has set up an incoming link to our site that looks like:
http://www.site.com/*our*-*services*/
I'd like to redirect it so it points it to:
http://www.site.com/our-services/
Ours is a Wordpress site so there's some rewrite stuff in our root htaccess file already. A rule that simply removes asterisks from the URL would do, but I can't figure out how to do that, so I tried the following - loosely based on copying the existing Wordpress rules - which isn't working:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/\*our\*-\*services\*/)
RewriteRule . /our-services/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
It looks like this rule is being ignored - the errant URL currently redirects you to our default Wordpress 404 page. Clearly, I am fail: what should I have put as the rewrite condition?
I'd remove your first RewriteCond and then just use this rule:
RewriteRule \*our\*-\*services\*\/ /our-services/ [L]
Well get them to change there incoming link.
Asterisks are reserved in urls, so in this case it shouldn't be used:
W3 Url Recommendation
Allowing asterisk in URL
So I have asked for help over at my weblog, scoured the internet, and pored over the examples you all have provided on here before, and I still cannot find an answer that works.
Simply put, I am trying to take all traffic referred to my site from Site A, and redirect it all to Page B within my domain. I have gotten the redirect to work perfectly, but I cannot get it to break out of an infinite loop. Any assistance would be greatly appreciated.
Code follows (though it has been "anonymized" from the specific pages I was using):
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !filename\.html$
RewriteCond %{HTTP_REFERER} ^http://online\.webpage\.com.* [NC]
RewriteRule (.*) http://www.wallsofthecity.net/year/mo/filename.html [L]
</IfModule>
Like I said, the RewriteRule works beautifully, but the first RewriteCond does not appear to be flagging when it is at the appropriate page, and just keeps redirecting folks, ad nauseum. I have been using this site: http://rexswain.com/httpview.html to check my code, and while useful, it has not given me any good answers.
Thanks for whatever help you can provide.
UPDATE:
So here is the .htacces file in its entirety, since that may make things easier:
<IfModule mod_rewrite.c>
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [R,L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
Redirect permanent /index.xml http://www.wallsofthecity.net/feed/
Redirect permanent /rss.xml http://www.wallsofthecity.net/feed/
Redirect permanent /atom.xml http://www.wallsofthecity.net/feed/atom/
Redirect permanent /12_tribes http://www.wallsofthecity.net/category/12-tribes
Redirect permanent /as_i_say_not_do http://www.wallsofthecity.net/category/as-i-say-not-do
Redirect permanent /bigotry_exposed http://www.wallsofthecity.net/category/bigotry-exposed
Redirect permanent /commercial_appeal http://www.wallsofthecity.net/category/commercial-appeal
Redirect permanent /cowardice_on_parade http://www.wallsofthecity.net/category/cowardice-on-parade
Redirect permanent /crosscountry_jaunt http://www.wallsofthecity.net/category/crosscountry-jaunt
Redirect permanent /digital_real_estate http://www.wallsofthecity.net/category/digital-real-estate
Redirect permanent /fools_and_jesters http://www.wallsofthecity.net/category/fools-and-jesters
Redirect permanent /for_hire http://www.wallsofthecity.net/category/for-hire
Redirect permanent /me_myself_and_i http://www.wallsofthecity.net/category/me-myself-and-i
Redirect permanent /musings_of_a_madman http://www.wallsofthecity.net/category/musings-of-a-madman
Redirect permanent /one-line_review http://www.wallsofthecity.net/category/one-line-review
Redirect permanent /patron_polity_of_perforation http://www.wallsofthecity.net/category/patron-polity-of-perforation
Redirect permanent /peoples_republic_of_kalifornistan http://www.wallsofthecity.net/category/peoples-republic-of-kalifornistan
Redirect permanent /sensor_ping http://www.wallsofthecity.net/category/sensor-ping
Redirect permanent /serenity http://www.wallsofthecity.net/category/serenity
Redirect permanent /simon_jester http://www.wallsofthecity.net/category/simon-jester
Redirect permanent /the_funnies http://www.wallsofthecity.net/category/the-funnies
Redirect permanent /the_mat http://www.wallsofthecity.net/category/the-mat
Redirect permanent /things_that_go_boom http://www.wallsofthecity.net/category/things-that-go-boom
Redirect permanent /toysgizmosgadgets http://www.wallsofthecity.net/category/toysgizmosgadgets
Redirect permanent /urk http://www.wallsofthecity.net/category/urk
Redirect permanent /window_on_the_world http://www.wallsofthecity.net/category/window-on-the-world
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Note: it still does not appear to be working. Test away :).
The first thing I'd try is changing the rewrite condition as follows:
RewriteCond %{REQUEST_FILENAME} !^.*filename\.html$
I think Apache is not supposed to force the match to start at the beginning of the string, but if it does (for whatever reason) and if that's the cause of your problem, this change will fix it.
As an alternative to that, just drop the first rewrite condition entirely and change your rewriting rule to
RewriteRule !year/mo/filename.html http://www.wallsofthecity.net/year/mo/filename.html
Edit: Let's try this instead (removed previous to limit post length):
External Redirect:
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [R,L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
# With an external redirect, the first RewriteCond catches our referrer redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
# These are always evaluated after mod_rewrite stuff, but use the original
# REQUEST_URI unless we explicitly passed it through to handlers later in
# the chain (via the PT flag)
Redirect permanent ... (trimmed)
Internal Redirect:
RewriteEngine On
# Force an external redirect to this page for referrals from that site
# This page *must* exist to prevent a loop (which it does, I checked :P)
RewriteCond %{HTTP_REFERER} ^http://mikeb302000\.blogspot\.com.* [NC]
RewriteCond %{REQUEST_URI} !=/2010/04/cruisin-for-a-bruisin.html
RewriteRule . /2010/04/cruisin-for-a-bruisin.html [L]
# This scenario performs no rewrite, so it should actually just be handled by
# the RewriteConds below (they won't match), but I didn't test that
RewriteCond %{REQUEST_URI} ^/(stats|failed_auth\.html).*$ [NC]
RewriteRule . - [L]
# With an internal redirect, we have to do an extra check to prevent this rewrite
# with our referrer redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0 !-f
RewriteRule .* /index.php [L]
# These are always evaluated after mod_rewrite stuff, but use the original
# REQUEST_URI unless we explicitly passed it through to handlers later in
# the chain (via the PT flag)
Redirect permanent ... (trimmed)
Edit (again):
Let's try a little diagnostics too...Can you put this above your other rules in the .htaccess file?
RewriteCond %{QUERY_STRING} diagnostic
RewriteRule . /?ref=%{HTTP_REFERER}&uri=%{REQUEST_URI}&matchable=$0 [R,L]