I'm having trouble with a htaccess mod_rewrite preventing php files from being executed directly.
The site seems to be running on an MVC framework (not 100% sure on which one unfortunately) and requests are being sent through a router class. I have a subdirectory which I want to exclude from the routing and have its files be executed directly.
Here is my current htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
I have tried excluding the subdirectory I want to execute directly with the following line but it made no difference:
RewriteCond %{REQUEST_URI} !^subdir/(.*)
Is there any way I can execute the php files in subdir directly? Any help would be much appreciated, thanks.
Have you tried with RewriteCond %{REQUEST_URI} !^/subdir/ ? The REQUEST_URI may start with a /.
RewriteCond %{REQUEST_FILENAME} !-f
This line allows you to execute files directly. If you are getting error 500, check your scripts for fatal errors / uncaught exceptions
Related
So I'm using the Flight PHP microframework (http://flightphp.com/) to do routing. My question is, how can I run the router from within a subdirectory? What I mean is, essentially, run it 'sandboxed' within a folder.
As in, a request to '/' just pulls the regular index.php file. But a request to '/flight/file' would load the URL using Flight.
I know you can't just dump it in a folder on the server and expect it to work because FlightPHP expects the URLs relative to the root.
Is there a way to run FlightPHP isolated in a directory with the rest of the website running regular PHP?
EDIT
I tried simply putting the .htaccess file into the subdirectory. This has the peculiar effect of causing the routes to still act as if they are from the root (e.g. /thing/otherthing/ when it should be /otherdirectory/thing/otherthing/ ) while simultaneously causing the 404 callback to not work. Not what I intended.
EDIT 2
Contents of .htaccess file, which are what is suggested by the Flightphp website:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
I know this is an old question but I've been doing something similar just by adding something along the lines of
RewriteBase /flight/
in the .htaccess file (before all of your rules) of the flight directory. Hope this helps someone else looking for the same thing. Example:
RewriteEngine On
RewriteBase /flight/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Sorry for giving you an answer that is not going to help a lot. I'm running Flight (also sandboxing) on a subdirectory. I've created an .htaccess just with the defaults in the subdir and Flight is now regarding this as it's root.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
Did you check the content of the .htaccess on the higher levels, maybe there is something that is blocking you
Does anyone know how to have a kind of URL Shortner?
for example, using PHP rather than having www.domain.com/page.php?seq=1435 to have www.domain.com/rGhpf
Does anyone have any ideas on what i can do with this - any help would be much appreciated
You can do this using Apache's Rewrite urls.
RewriteEngine On
RewriteRule ^seq/([0-9/]+)$ /page.php?seq=$1 [L]
That means you can goto http://domain.com/seq/1435 instead of http://www.domain.com/page.php?seq=1435
Ensure Apache's mod_rewite is available http://httpd.apache.org/docs/current/mod/mod_rewrite.html
Create a file called .htaccess in the docroot of the site (you can also do this in other directories with other rules)
If script is index.php all okay. No need to continue L=last
If script doesn't exist or directory doesn't exist redirect to index.php (we know its okay) and put requested script in $_GET['seq']
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?seq=$1 [L]
I have the following in my htaccess file:
RewriteEngine On
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
From rewrite.php I redirect to the correct pages depending on the url. Problem is that it redirects all files including css and js. I tried including these files but I now realise that was dumb of me. Should I redirect when there is and appropriate extension in the url? If redirecting is the way to go what method would be best? header location or HTTP_redirect?
Or is this not a good idea performance or work involved wise? I could go for something like this but I know next to nothing about apache and would rather not work with it right now.
RewriteRule ^(.*).css$ /includes/compressor.php?i=$1.css [L]
I previously had the following in my htaccess file:
RewriteCond %{REQUEST_FILENAME} !-f
I decided to remove this because:
I would not be able to include the header and other common files in the rewrite.php file. I would also not be able to have a database call in the rewrite file that would determine the page to include and to reuse the data for the page contents.
Unwanted files would be reachable such as service used only by external app.
The compression should be done once, and not for every request. You can then exclude requests from the URL rewriting if the corresponding file exists:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
How about redirecting only if the requested file does not exist on the server?
You could use the following rewrite conditions to achieve this.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ rewrite.php?data=$1 [L,QSA]
So if the request is for a CSS/JS/HTML/Image file that already exists on the server then no rewriting takes place and the request for the file is processed. If the file does not exist, it will run your rewrite rule to rewrite.php
Instead of working live on my site I've decided to try and work locally and install new version later on my live site.
So I'm working with Codeigniter and have the following struture.
/htdocs/kow(site)/kowmanager(cms)
When I load https:localhost/kow it loads the correct controller however for some reason its not reconizing that kowmanager is a sub directory of kow with its own application folder and it should be loading the default controller that is set in its routes file. When I load https://localhost.com/kow/kowmanager it loads a page that says index of /kow/kowmanager and then a link to the parent directory. Which isn't anything CI related.
Inside the kow directory this is my .htaccess file. Is this the problem?
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteEngine On
RewriteBase /kowmanager
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I'm using xxamp.
You need an index.php-page for each application as stated by the manual.
So I think you should copy your index.php to indexManager.php and in it change the application folder.
$application_folder = "kowmanager";
About the rewrite I am not sure but I think it is in line with:
RewriteEngine On
RewriteBase /kowmanager
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ indexManager.php?/$1 [L]
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
are you using a .htaccess to remove index.php from urls ?
is /htdocs/kow the base directory of your project where CI is installed ?
you need to put kowmanagers in the controllers directory, and specify which controller to call, I'm not sure CI supports calling default controllers from directories unless you specify it in the routes.php config file.
Anyway, please give further information if you want a precise answer.
What is in kowmanager and why do you have two rewrites? If your directory structure is how I assume it is, you might get away with just removing the second kowmanager directive from your .htaccess file.
Which folder is codeigniter in? All you want to do is rewrite the url to remove index.php, but unless you're mapping the url to codeigniter's index.php, it will never be able to load controllers.
Apache has a lot of info on url rewrite. Its a lot of reading and the behavior is always pretty finicky, but maybe it will help you:
Otherwise, more info will help us help you. P.S. I'd also tag this with apache, as that is where the problem is, and you're more likely to get people who know a lot about apache to view your question.
In an symfony project, is there any way to exclude a directory from being processed.
For example, I want to run a seperate php program in mysite.com/other_app
How can I exclude web/other_app folder from being processed by the symfony controller.
I've tried using
RewriteCond %{REQUEST_URI} !^/other_app/(.*)$
and
RewriteCond %{REQUEST_FILENAME} !-d
Symfony still processes the other_app which obviously isn't going to work properly. Any suggestions.Do I need to modify htaccess in other_app/.htaccess as well?
Also, let me add. When I access just the directory mysite.com/other_app I get the correct content. If I try and access anything below that, I get problems.
This works:
mysite.com/other_app
This does not:
mysite.com/other_app/page1
In the logs I get an error:
Action "other_app/page1" does not exist. Or something similar.
Ok. I figured it out. The .htaccess of the sub directory also needs to be modified.
In web/.htaccess use:
RewriteCond %{REQUEST_FILENAME} !-d
In the sub directory, web/other_app/.htaccess use:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /other_app/index.php [QSA,L]
This will work for wordpress and most any other php app that you want to run on a symfony site, but don't want to deal with trying to hack it into the framework.
Try adding:
RewriteCond %{REQUEST_FILENAME} !-d