I am having a problem using the .htaccess file. I have created the file and set it up so that I don't have to type file extensions in the URL. Unfortunately, this has created a new problem. I used to be able to type localhost/project and it would display index.php. But now if I type localhost/project it displays a Server Error 500. Does anyone know how I can solve this error?
.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
You need to also check the !-d with the request filename, may as well add all the right conditions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The problem is that /project is a directory, and mod_dir automatically redirects that to /project/, and that matches the first rule, so it turns to /project.php, which isn't a file.
Related
I'm trying to exclude "test" subfolder from following code i.e. "www.abcwebsite.com/test"
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
So when I create a subfolder in my directory it takes me back to "www.abcwebsite.com/404".
What I Want it to be like:
www.abcwebsite.com/file.php > www.abcwebsite.com/file should work
And also
www.abcwebsite.com/test (test being subfolder) should also work.
Thanks in Advance.
I have the following code:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} !^domain\.co
RewriteCond %{HTTP_HOST} !^[^.]+\.(domain\.co)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /somefile.php [L]
The bit that says somefile needs to be the filename that the user is on without the extension. So if they are at example.com/file, then file should be retrieved.
I have tried using the code below but it doesn't seem to work:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{HTTP_HOST} !^domain\.co
RewriteCond %{HTTP_HOST} !^[^.]+\.(domain\.co)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /$.php [L]
The bit that says .php in the rewrite rule should not be part of the filename retrieved and is manually added.
If there is no way to do this without getting the extension, how could this be done regardless of the extension?
You can add this rule above your existing rule to add .php extension:
RewriteEngine On
# add .php extension if corresponding .php file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} !domain\.co$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !/$ /somefile.php [L]
i have a htaccess rule that remove .php extension:
`RewriteEngine On
ErrorDocument 403 "Page not exist"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]`
the htaccess rule below redirects to stream.php file:
`RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /core/musicbox/stream/?tag=$1 [NC,L,QSA]`
Both rule works individually, but when combined,the rule placed below does not work.
You have the same conditions twice:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Which means:
!-f: File %{REQUEST_FILENAME} doesn't exist.
!-d: Directory %{REQUEST_FILENAME} doesn't exist.
If both are true and the next one (in the first set of rules) is also true:
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
Then the next rule will do a 301 redirect (R=301) and stop processing rules (L).
RewriteRule (.*)$ /$1/ [R=301,L]
Actually, maybe there's a missing ^ in there. It should be:
RewriteRule ^(.*)$ /$1/ [R=301,L]
Anyway, that means that the second set of conditions and the subsequent rule won't be reached:
RewriteRule ^(.*)$ /core/musicbox/stream/?tag=$1 [NC,L,QSA]
Probably you don't need those first R=301 and L flag. I think this is how it should look like:
RewriteEngine On
ErrorDocument 403 "Page not exist"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule ^(.*)$ /$1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /core/musicbox/stream/?tag=$1 [NC,L,QSA]
I am trying to do RewriteRule for subdirectory and I could not do it.
This is the code:
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} !-f
I use the following code with the above as well to remove the .php extension and add a trailing slash at the end.
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
I can access any page except index.php
I need someone to help me fix this issue so I can access the pages/files in the subdirectory.
Thank you
You need to explicitly check if the php file exists before you add the .php extension. And remember that rewrite conditions only apply to the immediately following rule, so you need to duplicate them if they need to apply to multiple rules. Also, you want all your redirects to happen before any routing or rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
# whatever subfolder rewrites you need can go here
i have a Problem with my .htaccess/mod_rewrite:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^sometest/(.+)/page/([0-9]+)$ index.php?main=sometest/$1&page=$2
RewriteRule ^(.+)$ index.php?main=$1 [QSA]
This does not work. If i visit my website the css files are not included.
I include them like "/assets/css/blabla.css".
If i delete just one RewriteRule (doesn't matter which one) it works. But i need both rules.
You must use skip flag skip|S=numfor this.
like :
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d [NC,OR]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} ^(.*)/([^/]+)\.([^/]+)$
RewriteRule ^ - [S=2,L]
RewriteRule ^sometest/(.+)/page/([0-9]+)$ index.php?main=sometest/$1&page=$2 [S=1,L]
RewriteRule ^(.+)$ index.php?main=$1 [QSA,L]
see this page for more information http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule