Access only mydomain_name/filename.html - php

I need to deny access for all excluded URLs:
mydomainname/filename.html
and redirect this request to:
mydomainname/index.php?page=filename.html
And the user should see in the address bar:
mydomainname/filename.html
And I want all styles and js scripts to work correctly.
How can I do this?
I am was trying this:
ErrorDocument 404 /
ErrorDocument 403 /
RewriteEngine On
RewriteBase /
RewriteRule ^(\w+)(?=\.).html$ index.php?page=$1.html
#if the request is for existent dirs, forbid the request
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule ^ - [R=403,L]
RewriteCond %{REQUEST_URI} !^/?$
RewriteRule !^index.php$ - [R,F,L]
Options All -Indexes
And I have server error 500.
Without "Options All -Indexes" js files can't be loaded and styles are not working.

So, my final .htaccess file:
RewriteEngine On
ErrorDocument 403 http://mydomainname
ErrorDocument 404 http://mydomainname
# Don't show directory listings for URLs which map to a directory.
Options -Indexes
# Set the default handler.
DirectoryIndex index.php
# Internal redirect
RewriteRule ^(\w+)(?=\.).html$ index.php?page=$1.html
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ / [L,R]
This solution is not deny access to mydomainname/include/myfile.php (if this file is exist), but it cover all other issues.

Search for rewrite condition in .htaccess file. You will get an idea for it. Or add RewriteRule ^(.*).(gif|jpg|png|jpeg|css|js|swf)$ /public/$1.$2 [L,NC] this.

Related

ErrorDocument 404 .htaccess not working with RewriteRule

.htaccess ErrorDocument 404 page not redirecting when I was using the rewrite rule. Getting 500 Internal Server Error.
Without RewriteRule the ErrorDocument is Working fine.
My Code
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /stores/$1 [NC,L]
ErrorDocument 404 /404.html
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /stores/$1 [NC,L]
The "problem" with this rule is that if you make a request for a non-existent file and that file not exist in the /stores/ subdirectory either then it will result in an endless rewrite-loop (hence the 500 Internal Server Error you are seeing) since it will attempt to rewrite as follows:
/not-exists (initial request)
/stores/not-exists (1st rewrite)
/stores/stores/not-exists (2nd pass)
/stores/stores/stores/not-exists (3rd pass)
etc.
You've not actually stated what you are trying to achieve here, but I assume /stores is a "hidden" subdirectory and you are intending to rewrite to actual files within the /stores subdirectory. This is the only way a rule like this would work with an ErrorDocument 404 directive defined in Apache.
So, when rewriting to the /stores subdirectory, you need to first check that the request would map to an actual file before issuing the rewrite. That way, any request that does not exist and does not map to a file in the /stores subdirectory will drop through to the Apache defined 404 ErrorDocument.
For example, try the following instead:
ErrorDocument 404 /404.html
RewriteEngine On
# Rewrite request to "/stores" directory if file exists in that directory
RewriteCond %{DOCUMENT_ROOT}/stores/$1 -f
RewriteRule (.+) stores/$1 [L]
# (OPTIONAL) Rewrite requests for root to "/stores/"
RewriteRule ^$ stores/ [L]
The <IfModule> container is not required (but you had the RewriteEngine directive outside of this container - which defeats the point anyway).
The RewriteBase directive is not required here if the .htaccess file is located in the document root.
It is structurally better to define your ErrorDocuments first.

Multiple Rewrite Rules for .htaccess, Redirect to www while including index.php

Specifically, I want to redirect all non-www pages to www, while also running an index.php file located in my root directory. To solve both of these problems I am using .htaccess.
I have already set up my site to run the PHP file to run in every directory. But the moment I add redirection from non-www to www it breaks.
The problem seems to be, that the multiple rewrite rules conflict with each other. Either one runs and the other does not, or the site just responds with a 500 error.
My question is, should multiple Rewrite rules be "combined" into one? Or am I just using those multiple rules wrong? (Or is it just some strange syntax thing I messed up? I have been working on this for a while haha)
Any help is very much appreciated.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L,QSA]
# Redirect to www.
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
</IfModule>
I found this works:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] #if not already index.php
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
RewriteRule . /index.php [L] #redirect to index.php
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www
</IfModule>
This cannot possibly "work" as written, as there are a number of errors:
You are missing the opening <IfModule mod_rewrite.c> directive. However, this <IfModule> wrapper is not required anyway and should be removed.
Line-end comments are not supported by Apache. Specifically, the following line will result in a 500 error due to "bad flag delimiters":
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
(UPDATE: If you are not seeing a 500 error response here, it's possible you are on a LiteSpeed server; not Apache? On LiteSpeed, this line-end comment appears to work as intended!)
Your external redirect (at the end) that redirects to www never gets processed for anything other than requests for directories (including the root) or real files (except index.php). This redirect needs to go first, before the existing rewrites. However, see the next point...
You are incorrectly using REQUEST_FILENAME (the absolute filesystem path) in the target URL - this will result in a malformed redirect. You could use the REQUEST_URI server variable instead (full URL-path), but note that you also have a double slash issue. So, it would need to be rewritten like the following instead:
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]
Minor points:
The RewriteBase is not being used here and could be safely removed. (Unless you have other directives that use this?)
Summary
Bringing the above points together we have:
RewriteEngine On
# Redirect to https://www
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Stop here if already index.php
RewriteRule ^index\.php$ - [L]
# Only if NOT a FILE (non-existent file)
RewriteCond %{REQUEST_FILENAME} !-f
# Rewrite to index.php (in the document root)
RewriteRule . /index.php [L]
Note that this still rewrites directories to /index.php, contrary to what your comment stated.
Test first with a 302 (temporary) redirect to avoid potential caching issues.
You will need to clear your browser cache before testing.
After lots of tinkering/research, I found this works:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L] #if not already index.php
RewriteCond %{REQUEST_FILENAME} !-f #only if NOT a FILE (directory / non-existent file)
RewriteRule . /index.php [L] #redirect to index.php
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) https://www.example.com/%{REQUEST_FILENAME} [R=301,L] #redirect to https://www
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "*"
</IfModule>

missing directories don't trigger Error Document Handling htaccess

Basically, I've got the following .htaccess file:
Options -Indexes
ErrorDocument 400 /index.php?error=400
ErrorDocument 401 /index.php?error=400
ErrorDocument 403 /index.php?error=400
ErrorDocument 404 /index.php?error=400
ErrorDocument 500 /index.php?error=400
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Enable sites without extentions
options +MultiViews
<IfModule mod_rewrite.c>
Options +FollowSymlinks
# Adaptive-Images -----------------------------------------------------------------------------------
# Add any directories you wish to omit from the Adaptive-Images process on a new line, as follows:
# RewriteCond %{REQUEST_URI} !ignore-this-directory
# RewriteCond %{REQUEST_URI} !and-ignore-this-directory-too
#RewriteCond %{REQUEST_URI} !img
# don't apply the AI behaviour to images inside AI's cache folder:
#RewriteCond %{REQUEST_URI} !ai-cache
# Send any GIF, JPG, or PNG request that IS NOT stored inside one of the above directories
# to adaptive-images.php so we can select appropriately sized versions
# RewriteRule \.(?:jpe?g|gif|png)$ adaptive-images.php
# END Adaptive-Images -------------------------------------------------------------------------------
# SEO Friendly Redirect Rules -----------------------------------------------------------------------
RewriteRule ^(admin)/?$ admin/ [L,NC]
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
RewriteRule ^([A-Za-z0-9_]+)/([A-Za-z0-9_]*)/?$ index.php?title=$1&ch=$2 [L]
RewriteRule /$ index.php [END]
As of right now, the rules and conditions all work great except for one thing. If I navigate to say: "example.com/nonexistingfolder" , it is using this rule to process:
RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
Which brings up a php error because the argument passed is invalid. Which is to be expected, because it doesn't exist. But if I no navigate to: "example.com/nonexistingfolder/index.php" , I'm correctly greeted with my 404 page defined at the top. (don't worry about the slashes, in the original these are permalinks)
ErrorDocument 404 /index.php?error=400
I don't know too much outside of what I've learned today about .htaccess and mod_rewrite. Is there some other way I should be going about this? Or am I missing other statements?
I've tried adding the following lines to my .htaccess with no progress:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
Either these don't help in my case (and I thought the second would for sure), or I am still doing something wrong.
I expected the code to be rather straightforward, however this has left me scratching my head for a couple of hours. Thanks for taking the time to read and ponder this with me! Have a good one.
EDIT: I added these three lines of code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? /index.php?error=400 [L]
Which works when I navigate to something that doesn't exist. Yet, it interrupts the other RewriteRule I have set: RewriteRule ^([A-Za-z0-9-]+)/?$ /index.php?title=$1 [L]
So now I am for sure at a loss at what to do.
To further exemplify:
Say I navigate to "website.com/nonexistingfolder". Which now because I added the three lines above, shows the 404 page correctly. However, because of the single RewriteRule above, if I navigate to: "website.com/indexquery" it will also show the 404. How can I tell my .htaccess to differentiate between the two?

htaccess redirect to a maintainance file

I have an htaccess in the public directory:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /sub/web1/
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# HERE I NEED SOM MAGICAL ONELINER TO REDIRECT STUFF TO MAINTENANCE.HTML
# BUT NO IDEA HOW TO WRITE IT ;(
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
and I want to add some rule inside, so when I uncomment the rule, all links that hit http://example.com/sub/web1/ or any files down that line, will be redirected to http://example.com/sub/web1/maintenance.html
I have tried to add:
DirectoryIndex maintenance.html
but this only redirects http://example.com/sub/web1/, if I have some subfolder or specific files like http://example.com/sub/web1/posts, it is useless.
Is there some oneliner that can even pull the domain name so it hasn't have to be typed absolutely? So, the example.com - or whatever domain - is not needed to type in the rule?
You can have a rule like this:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /sub/web1/
# uncomment line below to route everything to maintenance.html
# RewriteRule !^maintenance\.html$ maintenance.html [L,NC]
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# HERE I NEED SOM MAGICAL ONELINER TO REDIRECT STUFF TO MAINTENANCE.HTML
# BUT NO IDEA HOW TO WRITE IT ;(
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Although this has already been answered and accepted, it's worth mentioning that if your maintenance page is temporary, you really should be returning the right http response, otherwise search engines might drop or re-index pages incorrectly.
At my company we use something like this:
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}/holding/holding.html -f
RewriteCond %{DOCUMENT_ROOT}/holding/holding.enable -f
RewriteCond %{SCRIPT_FILENAME} !holding.html
RewriteRule ^.*$ /holding/holding.html [R=503,L]
ErrorDocument 503 /holding/holding.html
We create holding.html as the maintenance page, and then touch holding.enable to make the server switch to it without requiring a restart (not forgetting to rm it when we're done). The 503 return code on the rewrite rule, and the ErrorDocument 503 ensure that search engines see this a temporary outage and don't start de-listing our pages.
You can use the %{HTTP_HOST} variable to get the current domain that was used when hitting the given htaccess file - here's a handy cheat sheet of things:
askapache - cheatsheet
And some examples
askapache - examples
Although it's worth trying this out as it'll use root ^$ and rewrite that to the sub directory you want to use:
RewriteBase /
ReqwriteRule ^$ /sub/web1/

URL rewrite through an entry.php

I have two directory under my application directory
my_app:
.htaccess
lib:
entry.php
web:
css:
css_file1
css_file2
js:
js_file1
js_file2
My url will be like this http://example.com/my_app/my_path
Under the directory my_app, there are two directories web and lib.
If the my_path can be map to any file under the web directory, use it.
For example, http://example.com/my_app/css/css_file1 should map to my_app/web/css/css_file1.
Otherwise redirect it to lib/entry.php/my_path so that the the my_path can be accessed through $_SERVER['PATH_INFO'] The redirection should be internal. The redirection should not be visible to user.
I want to write configuration in .htaccess to accomplish it.
I wrote the code as given below.
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule (.*) ../web/$1
RewriteCond web/%{REQUEST_FILENAME} !-f
RewriteCond web/%{REQUEST_FILENAME} !-d
RewriteRule (.*) lib/entry.php/$1
</IfModule>
But I am getting 403 error.
I believe following should work for you in /var/www/.htaccess i.e. directly under $DOCUMENT_ROOT:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -f [OR]
RewriteCond %{DOCUMENT_ROOT}/$1/web/$2 -d
RewriteRule ^([^/]+)/(.*)$ $1/web/$1 [L]
RewriteCond %{ENV:REDIRECT_STATUS} !200
RewriteRule ^([^/]+)/(.*)$ $1/lib/entry.php/$2 [L]

Categories