Why Server Responds manifest.json Request With index.html? - .htaccess

.htaccess
# force traffic to https
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [OR]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# serve index.html for any unknown paths
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [QSA,L]
because the request returned is index.html browser throws error like this Manifest: Line: 1, column: 1, Syntax error.
All Requests For Script Or Css Work Except This One.

My guess would be that you are using a relative URL-path in your client-side JavaScript to this manifest.json file so the browser resolves this incorrectly (relative to the URL in the address bar, not the file-path on the server as you are perhaps expecting), resulting in a 404 (which routes the request to index.html).
You need to use a root-relative (starting with a slash) or absolute (scheme + hostname) URLs when referencing any assets from client-side code when you are rewriting the URL from different URL-path depths.

Related

Forwarding Path With .htaccess

I use cPanel hosting on https://files.example.com and I'd like https://files.example.com/anything-here to redirect to my main website and forward the path, so you'd end up on https://www.example.com/anything-here. Is this possible?
Note that I also have a forced SSL redirect inside my .htaccess file.
https://www.example.com is a Google Site.
My .htaccess file:
ErrorDocument 400 /index.html
ErrorDocument 401 /index.html
ErrorDocument 403 /index.html
ErrorDocument 404 /index.html
ErrorDocument 500 /index.html
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
https://www.example.com is a Google Site.
If the two sites are on different servers and you simply need to redirect everything from one host to the other and maintain the same URL-path then you don't appear to need anything in your .htaccess file at files.example.com except for the following mod_alias Redirect directive:
# Redirect everything to https://www.example.com/ and maintain URL-path
Redirect 302 / https://www.example.com/
The Redirect directive is prefix matching and everything after the match is copied onto the end of the target URL. So, a request for /foo is redirected to https://www.example.com/foo.
If, however, you have other hostnames pointing to your cPanel account then you'll need to use a mod_rewrite rule and check the requested hostname.
For example, at the end of your existing .htaccess file:
# Redirect every request for "files.example.com"
# to https://www.example.com/ and maintain URL-path
RewriteCond %{HTTP_HOST} ^files\.example\.com [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L]
Reference:
https://httpd.apache.org/docs/2.4/mod/mod_alias.html#redirect
https://httpd.apache.org/docs/2.4/mod/mod_rewrite.html#rewriterule
UPDATE#1:
But I just realised that it's also forwarding the path for files that do exist on my hosting. I onlt want it to forward invalid paths through to www.example.com.
In that case, you'll need to do it like this instead:
RewriteCond %{HTTP_HOST} ^files\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L]
The 2nd and 3rd conditions check that the request does not map to an existing file (or directory) before issuing the redirect.
Remove the first condition that checks the HTTP_HOST if it's not required.
UPDATE#2:
Is there a way to have it exclude "/URL" from this? If "URL" is specified in the path (example.com/URL/whatever) then I do not want this .htaccess rule to take place. I just want it to use my ErrorDocuments for this path.
If it's just the one pattern you want to exclude, ie. all URLs that start /URL then you can modify just the RewritRule directive. For example:
:
RewriteRule !^URL https://www.example.com%{REQUEST_URI} [R=302,L]
Any URLs that do not start /URL will be excluded. Note that this also includes /URLwhatever, not just /URL/whatever.

htaccess not redirecting all not found page or URL on site to 404 pages

Try to set 404 page not found error page by htaccess.
Issue is if we are searching for
https://www.rsseosolution.com/suraj.html ... Its redirect perfect to 404.php.
But if we are searching for
https://www.rsseosolution.com/suraj.php ... Its not redirecting to 404.php and giving showing simple text message "File not found.".
In short problem is if extension is not php then its redirecting fine but if .php then its showing File not found.
ErrorDocument 404 https://www.rsseosolution.com/404.php
Options +FollowSymLinks
RewriteEngine on
# ensure www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# ensure https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule seo-package-(.*)-n-(.*)\.php$ seo-package-detail.php?id=$1&name=$2 [NC,L]
RewriteRule case-studies/(.*)-(.*)\.php$ case-studies.php?project=$1&id=$2 [NC,L]
RewriteRule tutorial/(.*)-(.*)\.php$ tutorial.php?topic=$1&id=$2 [NC,L]
RewriteRule blog-page-(.*)\.php$ blog.php?page=$1 [NC,L]
RewriteRule seo-tutorial-(.*)\.php$ seo-tutorial.php?page=$1 [NC,L]
RewriteRule frequently-ask-question-faq-(.*)\.php$ frequently-ask-question-faq.php?page=$1 [NC,L]
What i am looking for is ....... redirect all not found or wrong URL to 404.php.
What i am missing here.
I think because we are rewriting some URLs with .php extension from htaccess and thats why for PHP its saying file not found but for other its redirecting perfectly to 404.php.
Is there any ways to set 404.php for all not found pages and URL without change any extension (.php) of previous written file by htaccess.
The problem is not to do with your current directives in .htaccess.
The problem is mostly likely due to your server config and the way PHP is implemented on your server. If requests for .php files are proxied to a backend server for processing as is often the case with FastCGI type configs then this 404 Not Found message is likely coming from the backend server and not your Apache server.
Ordinarily, you would solve this with the ProxyErrorOverride directive in the reverse proxy config:
ProxyErrorOverride On
If you don't have access to the main server config then you may be able to trigger the 404 early using mod_rewrite in .htaccess, before the request is sent to the proxy server.
For example, before your existing mod_rewrite directives:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule \.php$ - [R=404]
However, this is strictly a "workaround", if you have access to the server config then using ProxyErrorOverride (as mentioned above) is preferable.
Aside:
ErrorDocument 404 https://www.rsseosolution.com/404.php
By specifying an absolute URL in the ErrorDocument directive this triggers an external (302) redirect for the error document (an additional request), which is generally undesirable (and you need to manually set the 404 response code). It is far better to issue the error document as an internal subrequest instead:
ErrorDocument 404 /404.php

Site accessible with multiple URLs

I'm using the micro framework Silex on my website hosted on a VPS.
So, the site files are in the /site_name/public_html/ folder but, with Silex, the site must point to the /site_name/public_html/web/ folder.
In the public_html directory, I have the following .htaccess file :
Options -Indexes -MultiViews
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Redirect to https & www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
# Redirect incoming URLs to web folder
RewriteCond %{REQUEST_URI} !web/
RewriteRule (.*) /web/$1 [L]
</IfModule>
And, in the /public_html/web/ folder, the following .htaccess :
<IfModule mod_rewrite.c>
# Redirect incoming URLs to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Now, everything works fine but my pages are accessible with three different patterns :
example.com/page/ (the one I want to keep)
example.com/web/page/
example.com/web/index.php/page/
I have used the meta canonical to avoid duplicate content but I still want these last two options to not exist.
I guess I have something to change in both .htaccess files but I can't find what it is.
I would actually remove the .htaccess file in the /web subdirectory altogether and rewrite directly to /web/index.php in the root .htaccess file. By having two .htaccess files you are seemingly creating extra work. The mod_rewrite directives in the subdirectory will completely override the parent directives (by default), so your canonical HTTPS and www redirects are also being overridden.
(Presumably you had a RewriteEngine On directive in the /web/.htaccess file?)
Having removed the /web/.htaccess file, try something like the following in your root .htaccess file:
Options -Indexes -MultiViews
RewriteEngine On
RewriteBase /web
# Redirect to https & www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=302,L,NE]
# If /web or /index.php is present at the start of the requested URL then remove it (via redirect)
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(?:web|index\.php)/(.*) /$1 [R=302,L]
# Front-controller...
# Internally rewrite all requests to /web/index.php (uses RewriteBase set above)
RewriteRule index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The check against the REDIRECT_STATUS environment variable ensures we only test initial requests and not requests that have been later rewritten.
The <IfModule> wrapper is not required, unless your site is intended to work without mod_rewrite.
Note that a request like /web/index.php/page/ would result in two redirects. First to /index.php/page then to /page. Since this is an edge case I would consider a double redirect to be acceptable.
UPDATE: I've removed the "directory" check in the above as this would have prevented the document root (example.com/) from being rewritten to the /web subdirectory. This would have consequently resulted in a 403 if you didn't have a directory index document (eg. index.php) in the document root of your site. (However, requests for example.com/page/ should have still worked OK.)
Test with 302 (temporary) redirects and only change to 301 (permanent) when you are sure it's working OK - to avoid any caching issues in the browser. Be sure to clear the browser cache before testing.

How to rewrite a specific URL to 404 but allow access to that same URL by another rewritten URL?

I have a specific file (lets call this file the file-handler) on my server which should handle all requests to files with a specific extension. The file-handler itself should however not directly be accessible.
What I tried:
Place the file-handler in a password protected directory. Unfortunately any URL rewritten to that specific directory causes the browser (hence the server) to ask for the password, so this method doesn't work.
Rewrite direct access to the file-handler to the default 404 page, Unfortunately any URL rewritten to the file-handler causes mod_rewrite to serve the 404 page.
Source for second attempt:
RewriteBase /
RewriteRule dontallowdirectaccess\.xxx$ - [R=404,L]
#Only serve existing files to the file-handler
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.ext$ dontallowdirectaccess.xxx [QSA,L]
For the first 404 enforcing rule use THE_REQUEST variable. THE_REQUEST variable represents original request received by Apache from the browser and it doesn't get overwritten after execution of some rewrite rules unlike REQUEST_URI variable.
RewriteCond %{THE_REQUEST} /dontallowdirectaccess\.xxx[?\s/] [NC]
RewriteRule ^ - [R=404,L]
#Only serve existing files to the file-handler
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule \.ext$ dontallowdirectaccess.xxx [L]
So I figured out that their is actually another way to do this which doesn't require you to parse the {THE_REQUEST} variable.
#Only serve existing files to the file-handler (directories not included)
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule (^.+\.ext$) dontallowdirectaccess.xxx?file=$1 [QSA,L]
#If ENV:REDIRECT_STATUS is empty than someone must be trying to access the file directly.
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule dontallowdirectaccess\.xxx$ - [R=404,L]

.htaccess redirect to index.php

My website : theexample.com
I modified my .htaccess so that if visitors visits an unkown url it redirects directly to index.php with :
FallbackResource index.php
The problem is that it works only if there is one element after "theexample.com".
for example :
"theexample.com/shudhusdfuisdfhisdfh": works and redirects to
"theexample.com/sh/fuisd/fhisdfh": doesn't work and doesn't
redirect to index.php.
You must use absolute path of fallback resource not a relative one:
FallbackResource /index.php
Otherwise FallbackResource index.php will try to load index.php in the provided sub-path e.g. theexample.com/sh/fuisd/fhisdfh will try to load theexample.com/sh/fuisd/index.php which will cause failure as that path doesn't exist resulting in Internal Server Error.
This is rewrite. Redirect only non-files links =)
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ index.php
Example: url http://example.com/asdjivuhr/34tv3t will redirext to index.php
BUT if you have files(css,js) http://example.com/script.js will stay untooched if file exists!

Resources