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]
Related
My directory structure is like this and my document root is /
/
.htaccess
/public
This is all on my local xampp server.
The server is set up to listen for mydomain.local and in my hosts file I have pointed mydomian.local to localhost.
I am using the following .htaccess code and it works fine when using mydomain.local as the address.
The problem that I am having is that it does not work when I am using localhost/mydomain as the address
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php?path=$1 [NC,L,QSA]
How can I get it to work when using localhost/mydomain as the address?
The reason for this is because I am trying to register service worker but they only work over https or localhost.
Thanks in advance
I fixed it.
root folder .htaccess
RewriteEngine On
RewriteRule ^$ public/$1 [L]
RewriteRule ^(.*) public/$1 [L]
public folder .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
Thanks
You may also keep a single .htaccess file at root by changing a bit your rewrite rule to add an optional mydomain/ in match but not using it :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Add optional group
RewriteRule ^(mydomain/)?(.*)$ /public/index.php?path=$2 [NC,L,QSA]
# Same with a non capturing group
RewriteRule ^(?:mydomain/)?(.*)$ /public/index.php?path=$1 [NC,L,QSA]
By the way, you can use htaccess tester to check effects of rules rewriting on url
I am trying to use .htaccess file for my REST service but I found that the url are not actually redirected. The web server show
I wanted to use url such as
/api/v1/test
instead of
/api/v1/rest.php?request=test
I have already check mod_rewrite is acutally working.
My .htaccess file is as below
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule api/v1/(.*)$ api/v1/rest.php?request=$1 [QSA,NC,L]
I have already look at PHP redirecting problem and Sitemap redirecting
you can try this rule instead because it works with me :
RewriteRule api/v1/([A-Za-z0-9-]+)/?$ api/v1/rest.php?request=$1
try putting .htaccess file in htdocs directory and if the directory api location is
C:\xampp\htdcos\xampp\www\api
then put the full path in the rule
this site generates mod_rewrite for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^api/v1/([^/]*)$ /api/v1/rest.php?request=$1 [L]
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]
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.
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 :)