Yii 404 page - override parent htaccess - .htaccess

I'm using the Yii framework at my company site.
I have a frontend at domain.com/... and a Yii based app at domain.com/app/.. .
We recently added a 404 page to the frontend site, with htaccess's ErrorDocument handler at the frontend. Problem is, the Yii app inherits from that 404, which gives a bad page. How can I force it to use Yii's embedded error handling page?
Thanks in advance.

There are 2 good article talking bout this:
How to use default layout for error pages instead of errorXXX views
Handling errors using an action
The idea is, you create a action in out main Controller that will handle all page errors, so, you can change your htaccess to redirect your application to the Controller:
domain.com/site/error

Ended up using a RewriteCond redirection instead of ErrorDocument, resulting in a "rule" for the file to be redirected. Basically something like this:
RewriteCond %{REQUEST_URI} !^/app
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* /404.php
Basically this means, "if file requested is not valid file, use this rewriterule". The first condition is to make sure it excludes the "app" folder, so it handles its own errors.

Related

Why htaccess not working for mobile browser?

I have website (mzadkm.com) try to RewriteRule short url to app.php page .
So if user browse "mzadkm.com/app" will show "mzadkm.com/app.php" page
RewriteRule ^/app /app.php [L,R=301]
It's work on Computer , but on mobile browser give me 404 page
Any ideas
That probably is what you are looking for:
RewriteRule ^/?app /app.php [L]
The documentation clearly says, that the pattern in a RewriteRule get's applied to the relative path of the request if the rule is implemented inside a distributed configuration file. That means you actually want to match the path app and not /app here. Which is why your rule did not get applied. The ^/?app is a variant to accept both path notations, relative and absolut, which means the same rule can get implemented in the central configuration or likewise in a distributed configuration file (".htaccess").
I took the liberty to also remove the external redirection you showed ("R=301") since that most likely is not what you want, according to the phrasing of your question. Instead you want an internal rewrite .
You need to take care however that you do not implement a rewriting loop. Which would result in failing requests and an "internal server error" (http status 500).
One approach would be that:
RewriteEngine on
RewriteRule ^/?app$ /app.php [L]
Here another one:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?app /app.php [L]
Why things looked fine on your computer, but not on a mobile browser is unclear. Since the same rules get applied and the requests look the same there has to be another reason for that. I suspect you looked at a cached result of a previous attempt somewhere. Remember to always use a fresh anonymous browser window when testing. And to check the response you receive back inside your browsers network console.

.htaccess Redirecting url to a folder. Error on loading resources

I need to redirect from URL to a folder.
So, in the htaccess file:
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule ^(.*) http://www.example.org/portal/$1 [P]
But, when the portal app is requested it calls many .css .js .png files...
When these files are requesteds, the URL works like this:
www.example.com/portal/file1.css
www.example.com/portal/file2.js...
and I receive the 404(file not found)
It because when the app requests the files, the htaccess intercepts the call and reirect it to www.example.com/portal/portal/file1.css
So, i need when the exactly domain www.example.com is called, the rule redirect to www.example.com/portal. But when some resource of portal be called, the redirect must be do to www.example.com/resource. Internally it working like: www.example.com/portal/portal and i receive the 404 error
Help-me please.
Thanks
Probably you're using a relative path to your assets href="/portal/file.css" and the root url is www.example.com/portal
Try using the links without directory like href="/file.css"

File restriction to specific page

I'm trying to make a file only accessible when you're on a specific page.
Like download.php, the user can click a link to the file and the download starts without problems.
But if you go to the link in the browser directly it should not work.
Could I use .htaccess for this? or how would I do this?
This is possible with .htaccess by checking for the ${HTTP_REFERER}, which is the previous url you were on.
RewriteEngine On
RewriteCond %{HTTP_REFERER} !^http://www.example.com/download.php
RewriteRule myFile.zip - [R=403,L]
This checks if the previous page was download.php, otherwise it rewrites the request for myFile.zip to a 403 error page.
Note that it is possible to forge a referer by intercepting / creating your own request. This does not provide 100% security.

Custom URL rewriting not working in codeigniter

I use the code below in a Codeigniter htaccess file
RewriteRule ^([^.]+)-vacation$ /search?location=$1
When i access it with the url www.example.com/moscow-vacation,
it gives me a 404 error.
I have used the above htaccess code in a core php application. It works fine there.
Most likely, you don't have a search script. If the script is named search.php, you can use this slightly modified rule
RewriteRule ^([^.]+)-vacation$ /search.php?location=$1
If you have a controller named Search with a method location, you can either rewrite directly to
RewriteRule ^([^.]+)-vacation$ index.php/search/location/$1
or try
RewriteRule ^([^.]+)-vacation$ search/location/$1

htaccess - redirect all requests within subdirectory, except if a requested file exists

I'm developing an app in php, and I need to set up a pretty broad .htaccess redirect. I did some reading and tried to write the RewriteConds myself, but it's a bit above my paygrade - I'm hoping someone with more experience can help. Here's what I'm trying to accomplish:
The app is contained in www.example.com/app/. Don't redirect anything above this directory.
Some files exist in this directory that will need to be accessed. Currently these are /app/includes/* and /app/sb_pages/*. This will change and expand in the future, so I need an elegant solution that encompasses all existing files. It's fine if the redirect triggers within these directories when a file isn't found - all I care about is being able to access the files within without the redirect triggering.
All other requests should be redirected to /app/index.php, with the trailing url passed in the querystring. For example, a request to /app/path1/path2/ should redirect to /app/index.php?path=path1/path2/
The redirect should not be transparent. When the user requests /app/path1/path2/, I want them to believe they have remained there. They should not see the url change to /app/index.php?path=path1/path2/.
Just for added clarity, here's a few cases to elaborate:
/app/includes/sidebar.php should not redirect.
/app/includes/nothing.html does not exist - redirect is OK
/app/path1/path2/ should redirect to /app/index.php?path=path1/path2/. User should still see their current URL as /app/path1/path2/.
I hope I've explained it clearly and pre-empted most questions. If you need clarification, please don't hesitate to ask. Thanks in advance for the help!
Try adding this to your .htaccess file in your document root:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app/(.*)$ /app/index.php?path=$1 [L,QSA]
Note that if you want accesses to existing directories (as opposed to files) to also not be redirected, add a RewriteCond %{REQUEST_FILENAME} !-d above the rule.

Resources