I have the following .htaccess which is supposed to open /index.php for ALL requests, regardless of whether the requested folder or file exists:
RewriteEngine On
RewriteRule . index.php [END]
Unfortunately, this doesn't work on my localhost:
http://localhost:9000/file -> opens /index.php (GOOD)
http://localhost:9000/folder/file -> opens /index.php if /folder/ doesn't exist (GOOD)
http://localhost:9000/folder/file -> "Not Found" if /folder/ exists (BAD)
http://localhost:9000/file.php -> opens the file if it exists and gives a "Not Found" if it doesn't exist (BAD)
http://localhost:9000/folder/file.php -> opens the file if it exists and gives a "Not Found" if it doesn't exist (BAD)
My localhost is running on macOS via php -S localhost:9000 in Terminal.
From the comments to the question I understand that you are not using an apache http server for this, but rely on the http server built into php itself (php -S localhost:9000).
Please note that ".htaccess" style files are apache configuration files. I doubt that the builtin server has any idea about the purpose of those files ...
Instead that builtin server offers a feature to route all requests through a router script:
php -S localhost:9000 index.php
This is documented under "Example #3 Using a Router Script" in https://www.php.net/manual/en/features.commandline.webserver.php
Related
I have a server.php file in my Laravel project . in my computer I can command PHP -S localhost:8000 server.php so if I make an HTTP request to localhost:8000 it goes and read server.php file .or if I type this URL: localhost:8000/anything/anything it goes to read server.php file. but in my Linux server
if I type domain/anything/anything it goes to var/www/anything/anything and read index.php at there. but I want to read just server.php and pass those anything/anything to server.php file and does not count them as directory.what is the apache config to do this job?
You can use htaccess rewrite rules
RewriteRule ^anything/anything$ /server.php [L]
I suppose in your case you don't want it to match the exact string (anything/anything) so this might be the solution you're looking for:
RewriteRule ^(.*)$ /server.php?path=$1 [L]
So what the rule above will do is it will check everything that's entered after the url and pass it as GET (path variable) to server.php
I've tried to add the item below in .htaccess(wamp64\bin\apache\apache2.4.37\conf) and enabled Use URL Rewriting in Joomla however when I try to type abc.com in search engine address bar it will still redirect me to search page instead of redirecting me to the website. I've also performed checking and confirmed that .htaccess file is readable, the webpage is work when go to https://www.abc.co m however redirection still not work. I just need the page to redirect from http://abc.co m to https://www.abc.co m Is there any other way to do it? Or I'm doing it wrongly?? Please help.
RewriteCond %{HTTP_HOST} ^abc.com [NC]
RewriteRule ^(.*)$ https://www.abc.co m/$1 [L,R=301]
The redirect itself works great. The issue is where you've put the file.
.htaccess files are meant to be in the root folder and sub-directories of your website, not in the config files for Apache. Think of them as configuration files the server will find on-the-fly as it serves up documents, not static configurations loaded when the server starts (which are found in the "conf" folder you referred to). (See https://httpd.apache.org/docs/current/howto/htaccess.html)
For WAMP, the root folder is typically a folder called "www" and is normally located at c:/wamp/www. See Where is the web server root directory in WAMP? More commonly, though, this folder is called "public_html" (such as with LAMP, XAMP, MAMP, or default Apache installs).
I have my website inside the following path on my remote web hosting server:
~/public_html/website/app
and need to find a way to show it when typing:
mydomain.com
so without the need of typing the full path:
mydomain.com/website/app/
One solution that came to my mind was to create a index.php file inside my root folder to automatically redirect to ./website/app/:
<?php
header('Location: ./website/app/');
exit;
but then I need to rewrite the URL in order to go from:
mydomain.com/website/app/
to:
mydomain.com
and I did not find a way to do that!
So, what do you think I should do to obtain that result and show my website under /website/app/ when accessing:
mydomain.com
and do not show the /website/app/ folder names?
Thanks a lot!
edit:
I was forgetting that I also have the website/server path so I need a rule that keeps valid the requests containing mydomain.com/website/server/.
Plus, I cannot use:
Options +FollowSymLinks
on my hosting (for security reasons), it's overridden by:
Options +SymLinksIfOwnerMatch
You have two options to do that.
If you just want to show the /website/app directory for your root url / then you can use DirectoryIndex directive .
DirectoryIndex website/app
This will show you the index file from /website/app folder if you visit the your root URI ie : example.com/ .
If you want to server files and directories from the /website/app instead of the / folder then you can use a RewriteRule that rewrites your root level requests to the subfolders .
RewriteEngine on
RewriteRule !website/app /website/appâ„…{REQUEST_URI} [L]
This simple RewriteRule that I am using for practicing with .htaccess files works almost always:
RewriteEngine on
RewriteRule ^.*$ test.html
When I have the file flowers.html and I use http://localhost/flowers I get redirected to test.html, however when I rename flowers.html to flowers.php I get a 404 page with the message The requested URL /flowers was not found on this server. Does anyone know what causes this?
EDIT:
When I create an empty file called flowers it does redirect properly to test.html. What is going on here?
This does sound like a conflict with MultiViews, so try adding the following at the top of your .htaccess file to disable MultiViews:
Options -MultiViews
MultiViews is not enabled by default, so maybe this has been enabled in your server config?
When MultiViews (part of mod_negotiation) is enabled, a request for /flowers (no extension) will result in Apache searching for an appropriate file to return (based on mime-type) by trying various file extensions of files found in that directory. This is achieved with an internal subrequest before mod_rewrite runs.
However, it's not clear why this would be a problem in your case if you have no other directives? Since your directive simply rewrites everything to test.html (which should include any subrequests). (I was unable to reproduce this behaviour on my Apache 2.4 test server - hence my initial doubt.)
I want to develop a new website for a customer who is currently running a TYPO3 website on his server.
I'd like to exclude a subdirectory from the TYPO3 RewriteEngine (realurl?) so I have normal access to that subdirectory.
www.domain.com/dev
for example.
I have access to that site but as soon as I put a .htaccess file in that certain directory to protect it with a htpasswd. I get a TYPO3 Error:
Reason: Segment "dev" was not a keyword for a postVarSet as expected!
In .htaccess find this
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
and add your directory there
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|dev/) - [L]
I found the solution and it was anything but straightforward.
I needed to add this line to my .htaccess in the protected subdirectory:
ErrorDocument 401 "Not authorized"
Apparently there is a conflict between URL rewriting and Basic Auths in cases just like mine. The Server writes a "401 Unauthorized" Header and looks for an existing error document based on a pre-defined path which then gets rewritten.