I would like to redirect silently all requests of a domain to a subfolder if the folder / file doesn't exist.
Unfortunately some scenarios doesn't work or work non-silently.
works: http://mydomain.com/index.html -- it shows the content of production/index.html
does not work: http://mydomain.com/ -- it doesn't show production/index.html
works: http://mydomain.com/test/ -- it shows the content of production/test/index.html
does not work: http://mydomain.com/test -- it redirects the url (instead of a silent redirection) to http://mydomain.com/production/test/ and shows the content of production/test/index.html
Below is the .htaccess file:
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} !^/production/
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^(.*)$ /production/$1 [L]
It is because mod_dir is appending a trailing slash after mod_rewrite execution. Change your rules to this to overcome this issue:
DirectorySlash Off
Options -Indexes +SymLinksIfOwnerMatch
DirectoryIndex index.php index.html
RewriteEngine on
RewriteBase /
# add a trailing slash for directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{THE_REQUEST} \s/+([^.]*?[^/])[?\s]
RewriteRule [^/]$ /%1/ [L,R=302,NE]
# take care of landing page:
RewriteRule ^/?$ production/ [L]
# Don't apply to URLs that go to existing files or folders
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all those to insert /production folder
RewriteRule ^((?!production).+)$ production/$1 [L,NC]
Related
I'm trying to hide a subdirectory from the URL with .htaccess. I've got a PHP script which is executed everytime a client enters the website. It's index.php in root Apache directory. This script determines the language to use in the website and redirects to the target directory. English is my default language so what I need "en" directory to be hidden in the URL and at the same time redirect all the URL requests from root directory to "en" folder so they don't produce a 404 HTTP error. I partially achieved this with the following lines:
#Remove en/ directory from URL
RewriteRule ^$ en/
#Forward all the requests to en/ directory
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ en/$1
When requests are forwarded to "en" directory this is displayed again in the URL. This would be the flow:
User types: http://domain.com
index.php Redirects to http://domain.com/en because that's the detected language
.htaccess Rule hides /en directory, leaving the URL http://domain.com
Client types http://domain.com/panel to access their user panel
.htaccess Redirects to http://domain.com/en/panel
How can I remove "en" directory leaving the URL http://domain.com/panel after the last action in the list? Is there a better way to manage this behavior?
Current rules:
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^/?$ en/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^((?!en/).*)$ en/$1 [L,NC]
#Prevent direct access to PHP Scripts
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule \.php$ - [R=404,L]
#Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Have it this way:
Options -Indexes +FollowSymlinks -MultiViews
ErrorDocument 404 /404.php
RewriteEngine On
#Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]
# add a trailing slash if public/$1 is a directory
RewriteCond %{DOCUMENT_ROOT}/en/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
#Prevent direct access to PHP Scripts
RewriteCond %{THE_REQUEST} \.php[?/\s] [NC]
RewriteRule ^ - [R=404,L]
RewriteRule ^/?$ en/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!en/).*)$ en/$1 [L,NC]
Clear your browser cache and test your flows.
Below is the content of my .htaccess file on my site server.
# 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>
# END WordPress
I would like to block access to file requests in the folder notforyou (example of a request below):
http://my-cool-site.com/wp-content/uploads/notforyou/path/to/file.pdf
What rewrite conditions and rules can I add so that any file request from the folder like this can be redirected to another web page?
I added something like this at the top of the .htaccess but the files in notforyou can still be accessed:
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^wp-content/uploads/notforyou/(.*)$ http://example.com/ [NC,R,L]
add below code into your .htaccess
RewriteEngine On RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?yourdomain.com/.*$ [NC]
RewriteRule \.(gif|jpg|pdf|png)$ http://www.yoursite.com/hotlink.gif [R,L]
You’ll need to change you yoursite.com to your domain name, and change hotline.gif to an image file on your server that explains hotlinking is disabled on your site.
I had an error in the past "404 Not Found" when writing www.website.com/page instead of www.website.com/page/
And I corrected it by adding the following code to the .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ %{REQUEST_URI}/ [L,R=301]
</IfModule>
But now, it didn't render an xml page, because a trailing slash was added to the link
Example: www.website.com/post-sitemap.xml/
I want to prevent any errors in the future by simply adding a command on the .htaccess so it doesn't add the slash to links ending with: .html .php .xml .css ... etc
Thank you
You can use this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.[^.]+$
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
I want to redirect URLs of this form:
/page.html?variable=value&othervar=true&thirdvar=100
To this:
/page/?variable=value&othervar=true&thirdvar=100
So basically I just want to replace the .html in the middle of the URL with a forward slash, but I need to preserve the get string that comes with it. This is what I tried:
RewriteRule ^page.html(.+)$ /page/$1 [L,R=301]
But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]
# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]
I usually use this htaccess file to remove index.php from my URLs in ExpressionEngine
AddType video/ogg .ogv
AddType video/mp4 .mp4
AddType video/webm .webm
AcceptPathInfo On
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymLinks
# Looks for files and directories that do not exist
# and provide the segments to the index.php file
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^/index.php
RewriteCond $1 !.(css|js|png|jpe?g|gif|ico)$ [NC]
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
While that works great, before we move this site into production, we're directing all traffic to the given url to another via this htaccess file
RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteRule ^(.*)$ http://www.anotherdomain.com/ [R=301,NC]
My own ip address is replacing the localhost call so that I can access the site.
Basically what I'm looking for is a combination of these 2 that will remove index.php from my URLs for me but still redirect everyone else.
Thanks,
Steven
Found that this works great:
RewriteEngine on
# If your IP address matches any of these - then dont re-write
RewriteCond %{REMOTE_ADDR} !^127\.0\.0\.1
RewriteRule ^(.*)$ http://www.anothersite.com/ [R=302,L]
# do not rewrite links to the assets and theme files
RewriteCond $1 !^(assets|themes|images)
# do not rewrite for php files in the document root, robots.txt etc
RewriteCond $1 !^([^\..]+\.php|robots\.txt|crossdomain\.xml)
# but rewrite everything else
RewriteRule ^(.*)$ index.php/$1 [L]