Apache mod_rewrite ignore some real directories - php

That's my root directory:
css
img
js
includes
templates
api
and that's my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?ur=$1 [L,QSA]
It works fine but when I inform the url a directory exists, the Apache open the directory, when in fact it should inform the URI for ur parameter.
For example:
mydomain.com/test-uri/ goes to ur parameter.
mydomain.com/api/ apache loads the api directory.
I did some research, but the most we got was to consider all directories when I need only pass the api directory as parameter.

Remove the -d check:
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?ur=$1 [L,QSA]

Related

Remove sub folder url in htaccess

I have got it right to remove my subfolder from the url but its making my other root sub folders not pick up.
Eg...
www.blah.com/templates/index.html is now www.blah.com/index.php
but my www.blah.com/systemfiles/signin.php gives an error
I also have www.blah.com/systemfile/signin.php.
I can not access other sub folders.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]
The four lines you added says:
Follow symlinks
Start the enginge
If not /templates is in the URL
Treat the URL as /template was added
That is why it does not work for you. This strategy is kind of weird... But to make it work with your example you should check if the url points to a file or folder.
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/templates
RewriteRule ^(.*)$ templates/$1 [L]

redirect all files to index.php

I try to redirect all files and folders to index.php for an MVC routing but it working only from index.php like learn.delapiata.ro/index.php/i/can/put/anything/here/and/redirect
I wish to work without index.php: learn.delapiata.ro/controllers/any_php_file.php
I am using this in .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ /index.php [L]
Thank you.
What you have would already work - but you only need the -f rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
These rules simply mean: If the request does not point at a file that exists - send the requeset to index.php
Note that htaccess files only take effect at all if they are enabled in the apache config. If for example the htaccess file is modified to contain an error (put "asdf" in it) - and you don't get a 500 response - Apache isn't reading the .htaccess files and this is why it "doesn't work". To fix that, put AllowOverride All somewhere appropriate in your apache config file and restart apache.

Rewriting all request to index.php in the same diectory

I want all the request to be rewrited to index.php so I used this htaccess code.
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
It works fine when site is hosted on home directory. But suppose if I move my site to a subdirectory xyz and move all my files including .htacess file inside this directory. Then if I access http://example.com/xyz/some_page, the request is not redirected to /xyz/index.php.
So, How can I make this rewrite work even on subdirectories as in my case.
Update:
I forgot to mention the directory xyz as in my case is likely to change frequently. So, this directory doesn't need to be hard coded in the rewrite rule
Remove the RewriteBase
If you don't need the rewrite base - just remove it. A working example based on the one shown in the question would be:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
Note that the first rewrite rule has been deleted as it is logically equivalent to RewriteCond %{REQUEST_FILENAME} !-f (do not rewrite requests for a file that exists).
Consider also deleting RewriteCond %{REQUEST_FILENAME} !-d as this prevents urls like /this/folder/exists being sent to index.php. Most relevant if Directory listings are enabled but not desired.
change your base url for rewrite
RewriteBase subdir/
or try with full url
RewriteBase http://www.examle.com/subdir/

Apache, .htaccess - Not working locally

I have a rewrite rule in my .htaccess.
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]
Which redirects mysite.com/mypage to mysite.com/user_home.php?domain=mypage
This works fine on my shared host, but when working locally, my site is located in www/mysite/ and instead of redirecting to www/mysite/user_home.php?domain=mypage it tries to redirect to www/user_home.php?domain=mypage which doesn't exist.
I get the following error in my Apache Logs
[error] [client ::1] script 'C:/wamp/www/user_home.php' not found or unable to stat
How can I alter my .htaccess to make it direct to the file in the current directory using a relative path instead of an absolute path..
You just need to remove leading slash from the target URL in your rule like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ user_home.php?domain=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f is also needed to prevent infinite looping.
Try this code :
RewriteBase /mysite/
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [L,QSA]
You just have to add a RewriteBase with the path of the root folder of your website.
The best solution would be to add checks if there is a file called like this and set a RewriteBase:
RewriteEngine On
RewriteBase mysite/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+?)$ /user_home.php?domain=$1 [QSA,L]

.htaccess all request to other file

Currently I have the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
</IfModule>
Which works allmost perfect.
It rewerites urls like http://domain.com/something/ to the public/index.php file, like a charm, except when it is a file, just like it should.
However http://domain.com (without any path appended) (there is no index.php in the root, so it gives a 404 at the moment) is not being rewrited, how can I change this .htaccess so it rewrites this url too?
The index file is in public/index.php I want it to load that file through the use of .htaccess
Thanks
I believe to rewrite the root, you can simply do something along the lines of:
RewriteRule ^$ location/of/root/file [L]
You could try:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php
RewriteBase should prepend the rule pattern with a leading slash, forcing it to match the root path.
Untested!
What you have there is inspired by WordPress?? It's a bad idea as it tell Apache to always check if the path is a file or a directory before redirecting.
I have something like this
RewriteEngine On
RewriteCond %{REQUEST_URI} !^.*/(css|images|javascript)(.*) [NC]
RewriteCond %{REQUEST_URI} !\.(swf|ico|php|xml)$ [NC]
RewriteCond %{REQUEST_URI} !robots.txt
RewriteRule (.*) index.php?page=$1&%{QUERY_STRING} [PT]
The first condition restricts this redirect from working in specific folders.
The seconds does it for specific extensions.
You can guess what the third does :)

Categories