I have encountered a problem, while implementing MVC design patterns for my website. I have a folder named "svce" inside the folder /www (which is my DOCUMENT_ROOT). I created a .htaccess file and wrote the following codes inside it:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$index.php?url=$1 [QSA,L]
Then I edited the httpd.conf file and uncommented the LoadModule rewrite_module modules/mod_rewrite.so.
Even after doing this, RewriteEngine is not working. When I try to access application using http://localhost/svce/blahblah.php, it gives me a default 404 error page.
IT should have shown me only the index page inside the /svce directory.
You're missing a space:
# here -----------v
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Without that space, the rule wants to rewrite a URI that looks like /something/$index.phpurl=$1 and rewrite it to [QSA,L], which is probably not a valid resource.
Additionally make sure you have AllowOverride All set in your server config.
Related
In the past, I have got this working no problems at all, but for some reason on my new server I just can't get it to work.
I have the following .htaccess file in the root of my application
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I have also enabled mod_rewrite and have confirmed this with php_info()
my site is located at /var/www/html/test
Is it possible that although mod_rewrite is enabled that it is not working and if so, how can I test it?
On some server implementations, you'll need to wrap it within <IfModule> tags.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Also check your httpd.conf file to ensure it has:
Options FollowSymLinks
AllowOverride All
It'd also be worth checking to makesure the .htaccess file isn't being overridden by another .htaccess file.
A simple way to test if .htaccess is working
Simply put the following in your .htaccess file:
deny from All
What this does is deny access to your site from everyone. If you are presented with a 403 forbidden when trying to access the site - the .htaccess file is being included. If not - see above.
I use this on my codeigniter setup
RewriteEngine on
# prevent these directories from hitting CI
RewriteCond $1 !^(index\.php|images|assets|robots\.txt|crossdomain\.xml)
# route everything else to the index.php
RewriteRule ^/(.*)$ /index.php/$1 [QSA]
(my setup is done in the virtualhost of .conf and not in .htaccess, though they are usually about the same configuration)
I am trying to come up with a rewrite rule, but I am having problems.
What I need - any url that starts with /services/XXX to be redirected to /services/api.php?service=XXX
I also want to ignore any files or folders that might also match.
What I have so far:
RewriteRule ^services/([a-z]+)$ /services/api.php?service=$1 [NC, L]
But this does not work at all, it shows a 404 page saying that file is missing when I test it.
Any help is much appreciated.
To ignore the files that exists just ignore the "RewriteCond"s.
Just rewrite everything that comes into services/ to api.php?service ...
Put this into your services folder.
Be careful with your encoding, save the file as .htaccess with a encode that your server reads.
# .htaccess mod_rewrite
RewriteEngine On
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]
If you don't need to avoid Files, then just go to:
# .htaccess mod_rewrite
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ api.php?service=$1 [QSA,L]
I have the following API endpoints:
/api/project
/api/project/start
/api/project/end
These endpoints are supported by a file structure that looks like:
/api/project.php
/api/project/start.php
/api/project/end.php
I am currently using .htaccess to make the .php extension optional to the API user.
I noticed that if I hit /api/project without the .php extension, I get taken to the project folder on Apache instead of the project.php file.
Is there anyway to account for this in the .htaccess file or through a different method?
My current .htaccess rewrite rules
Options -Multiviews
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1/$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
I was able to solve this problem by adding DirectorySlash Off to my .htaccess file.
Apache automatically adds a trailing slash to the end of URLs if a directory by that name exists.
Once you update your .htaccess file and restart your server, you need to clear your browser cache (since most browsers will add the trailing slash to the end of your request if previous requests were automatically redirected).
I know this question has been asked like a 1000 times and I have probably tried like a 1000 suggestions as well, still no go.
I would like to remove the php file extension and substitute it with a slash (the slash is not so important but to remove the extension is).
My project is located here:
localhost/~fn/MyProject/
It contains two folders, public and includes. So all the public files are in the public folder: localhost/~fn/MyProject/public/index.php
I have tried so many suggestions already but most of them simply don't work. I am getting either a Internal Server Error, Forbidden or 404. I am putting the .htaccess to the public folder. Mod rewrite is on. No success with anything on stackoverflow and neither external resources ( e.g. http://alexcican.com/post/how-to-remove-php-html-htm-extensions-with-htaccess/ ). For example using the rewrite rules from the metioned webpage shows me 403 Forbidden to even access the index.
Any hints of what I may be doing wrong? I am really stuck. Thanks a lot!
If your htaccess is in public project folder, try with this code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{THE_REQUEST} ^.*public/(.+)\.php
RewriteRule ^(.*)$ /~fn/MyProject/public/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ /~fn/MyProject/public/$1.php [L]
If the directory has an Index.php in it, the url will not show the file name when you browse to that folder. example.com/index.php would just show as example.com. You could use PHP includes to pull in each page to the index page in order to hide the file names but this isn't the best practice in general.
Put this in a .htaccess file in localhost/~fn/MyProject/ (so the file will be localhost/~fn/MyProject/.htaccess):
RewriteRule ^(.+?)\.php$ $1/ [QSA,L]
This captures anything ending in .php, strips off the .php, adds a /, and appends any query string as needed.
If you want only remove extension ".php" from index.php you should use mode_rewrite
RewriteRule ^index.php$ index // or
RewriteRule ^index.php$ /
But best way is implements mechanism to rewrite all urls and manage in your front script (index.php);
If you want use mode_Rewrite you shuold check if your mod_Rewrite is enabled on your Apache server. Go to apache.conf and check if line
LoadModule rewrite_module modules/mod_rewrite.so
is uncomented, dont forgot restart apache!
than you can On rewrite rule type in your apache.conf
<IfModule mod_rewrite>
RewriteEngine On
</IfModule>
or include something like in .htacces
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php?%{QUERY_STRING} [L]
Now all signs from urls will going to your once index.php. Here you can do with this everything what do you need to do for example u can call some controller.
If you get 403 Forbiden dont forgot check chmod of the file.
To remove the .php extension from a PHP file for example yoursite.com/demo.php to yoursite.com/demo, you can add the following code to the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I have a website with this in the .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?menu=$1 [L,QSA]
#DirectoryIndex index.php
My problem is even, if I change a single letter the website is rendering a 500 Error. Even if I empty complete file, it still shows me the 500 Error.
What I want to do is, there is a page like this on the domain
http://www.example.co.uk/brochure/generate.php
The file generate.php does exist in /brochure/ directory.
But still the generate.php does not load and it is loading the index.php file.
Any help?
Try the following which I know should work:
<IfModule mod_rewrite.c>
#Turn the Rewrite Engine ON
RewriteEngine On
#Set the base where to rewrite the requests
RewriteBase /
#Allow direct file access
RewriteCond %{REQUEST_FILENAME} !-f
#Allow direct directory access
RewriteCond %{REQUEST_FILENAME} !-d
#Disallow certain files.
RewriteCond %{REQUEST_FILENAME} !brochure\/generate\.php$
#Rewrite URLS To GET[menu] and make sure this is the last rule.
RewriteRule ^(.*)$ index.php?menu=$1 [L]
</IfModule>
if you do not see any rewriting taking palce then check to see the module for rewrite is installed.
If an empty file triggers a 500 status code as well, then the error is somewhere else: you are not editing the real .htaccess file being used by Apache, the error comes from the application, etc.
In any case, you should find the error log: that's where the exact details are shown. In Linux, it's normally under /var/log/httpd. In Windows, it can be under %ProgramFiles%\Apache Software Foundation\Apache2.2\logs. If it's a shared hosting account, you'll probably have an "Errors" or "Logs" feature in your control panel.