Before posting here I've tried solutions URL1, URL2 but not fixed yet. My .htaccess code like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
In my application $_GET and $_POST used and file structure in htdocs like this:
shopping ->(main folder)
css(folder)
images(folder)
js(folder)
.htaccess
file1.php
file2.php
:
:
file9.php
So how I can get rid from removing .php in URL
Check that .htaccess is applied in the first place — see here;
Make sure that your configuration allows overriding at least FileInfo in .htaccess files (you may also do AllowOverride All):
<Directory "/var/www/html">
AllowOverride FileInfo
</Directory>
See this question for details.
Related
I am trying to redirect my project folders one level up in stucture.
I would like to change URL adresses as follows:
http://localhost/my-website/projects/project1/
http://localhost/my-website/projects/project2/
...
to
http://localhost/my-website/project1/
http://localhost/my-website/project2/
...
In root folder I do not have these project folders, they are in "projects" folder
the structure is like this
/root/
/projects/
/project1/
index.php
/project2/
index.php
/project3/
index.php
index.php
.htaccess
So if user enters URL http://localhost/my-website/project1/, the URL stays the same but it works as if he entered the full adress. Is this achievable? I tried so many options so far and I am still not able to do it...
First of all, don't use a .htaccess file, if you can modify the Apache config files.
It's better to add these types of configuration inside the vhost config instead.
With that said:
.htaccess
<Location "/">
AllowOverride None
Options FollowSymLinks
Require all granted
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteRule ^project(.*) projects/project$1 [QSA,L]
# RewriteRule ^project([0-9*]?)/(.*) projects/project$1/$2 [QSA,L]
</IfModule>
</Location>
/my-website/project1/ becomes /my-website/projects/project1/
/my-website/project1/whatever becomes /my-website/projects/project1/whatever
Uncomment the one that is more up your ally,
the first one is more generic and catches all after project,
the second one is more specific and looks for a number.
I have two problems actually:
First, I’m trying to redirect several short URLs to a single page with more actions, like this:
RewriteEngine On
RewriteRule ^/login?$ ^login.php?action=login&next=$1 [L]
RewriteRule ^/reset?$ ^login.php?action=reset&next=$1 [L]
This is being written in the .conf file inside <Directory>. The problem is that the first rule gets executed, while the second doesn’t and I can’t figure why.
I also tried writing them like this:
RewriteCond %{REQUEST_URI} /login$
RewriteRule ^login.php?action=login&next=$1 [L]
RewriteCond %{REQUEST_URI} /reset$
RewriteRule ^login.php?action=reset&next=$1 [L]
I should probably mention that login.php does not reside in the root directory, but in different subdirectories.
What am I doing wrong and how can I fix it?
The second issue I have is that if I put an .htaccess file inside the root directory, the rules in the .conf file don’t get executed anymore.
Inside the .conf file I have these rules:
<Directory>
Options Indexes FollowSymLinks ExecCGI Includes MultiViews
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
</Directory>
Why is this and how can I fix it?
Just to extend on from my comments above. Place these rules in your site root .htaccess or in httpd.conf file:
Options -MultiViews
RewriteEngine On
RewriteRule ^/?login(?:/(.*))?$ subdir1/login.php?action=login&next=$1 [L,NC,QSA]
RewriteRule ^/?reset(?:/(.*))?$ subdir1/login.php?action=reset&next=$1 [L,NC,QSA]
Option MultiViews (see http://httpd.apache.org/docs/2.4/content-negotiation.html) is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So if /file is the URL then Apache will serve /file.html.
I want to do routing in code-igniter and i have done the below steps also
but it is not working.I have done all the required settings also but then also
its not working.Please let me know is i am missing any thing or not.
Step 1
Add This in .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
Step 2:
Remove index.php in codeigniter config
$config['index_page'] = '';
Step 3
Add this in route.php
$route['Viewsignup'] = 'Login/index/Viewsignup';
My url is like that:
http://localhost/machinetest/index.php/Login/Viewsignup
I want this url:
http://localhost/machinetest/index.php/Viewsignup
Please help me,
Thanks in Advance
.htaccess should not be issue here. Try this
$route['Viewsignup'] = 'Login/Viewsignup';
This will take you into Login controller's Viewsignup method when your URL looks like http://localhost/machinetest/index.php/Viewsignup
And for removing index.php from URL just update your .htaccess with following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Now http://localhost/machinetest/Viewsignup URL will work too
On some servers, Apache is configured to ignore some or all directives in .htaccess files. This is for security reasons. The AllowOverride directive controls which features will be allowed in .htaccess files. For example AllowOverride None can turn off htaccess files for a folder and its subfolders.
Check your Apache configuration file for which AllowOverride directive is applied to the directory containing your problem htaccess file.
If you’re not sure which configuration file to look in, start with the main Apache configuration file httpd.conf or apache2.conf. If your website is configured in a file included by httpd.conf (e.g. a virtual hosts configuration file), you will need to look in that file. See Location of httpd.conf on CentOS, Ubuntu, Mac and others to locate your httpd.conf.
To enable using a .htaccess file, change AllowOverride None to AllowOverride All.
For example, for a CentOS 5.3 server, I needed to change the AllowOverride setting in the file /etc/httpd/conf.d/virtualhosts.conf.
httpd.conf before:
Options FollowSymLinks
AllowOverride None
httpd.conf after:
Options FollowSymLinks
AllowOverride All
Be aware that enabling htaccess files has security implications, as htaccess files override your Apache configuration. For example, if your site provides uploads, a hacker could potentially upload a .htaccess file to your server and use it to gain access to your server. There are options to AllowOverride that restrict the directives that will be used from a .htaccess file. See the documentation for AllowOverride.
refer http://smartwebdeveloper.com/apache/htaccess-problems
I'm on a win8 machine running XAMPP. I have a virtual host directory set up with a .htaccess file. I have been researching how to rewrite urls and I found the RewriteEngine module. In my httpd.conf apache file, the module was already enabled:
LoadModule rewrite_module modules/mod_rewrite.so
It seems like the next step was to update my .htaccess file like so:
php_value register_globals off
DirectoryIndex default.php index.php
ErrorDocument 404 /filenotfound.html
RewriteEngine On
RewriteBase /
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
RewriteRule ^Blog//Categories$ Blog/Categories.php [L]
RewriteRule ^Blog//([^/.]+)/?$ Blog/Posts.php?val=$1 [L]
I have followed a couple SO questions (config and rewriting w/ params), but am unable to get even the easiest rewrites to work. I have restarted apache a couple of times with no results.
While I'm here, this is my folder structure boiled down to everything relevant:
root
.htaccess
Blog
AuthorPanel.php
Categories.php
Post.php
Posts.php
Default.php
About.php
Work.php
And these are the rewrites I am looking to achieve (I have already tried most of them):
site.com/Default.php => site.com/Home
site.com/About.php => site.com/AboutMe
site.com/Blog/Categories.php => site.com/Blog/Categories
site.com/Blog/Posts.php?id=3&val=Android => site.com/Blog/Android
site.com//Blog/Post.php?id=4&title=Working+with+ActionBar => site.com/Blog/Working-with-ActionBar
Update 1 In httpd-vhosts.conf I even tried using the RewriteEngine on and rewrite rules, with no luck either:
<VirtualHost *>
DocumentRoot "C:/Users/ben/Documents/PHP/benWIT"
ServerName benWIT.local
<Directory "C:/Users/ben/Documents/PHP/benWIT">
RewriteEngine on
Order allow,deny
AllowOverride all
Allow from all
Require all granted
RewriteRule ^Home$ Default.php [L]
RewriteRule ^AboutMe$ About.php [L]
RewriteRule ^Work$ Work.php [L]
</Directory>
</VirtualHost>
Since you are using htaccess then you will need to make sure AllowOverride is set to All in your httpd.conf file:
AllowOverride All
This will allow you to use htaccess files. Having said that, as a general rule you don't want to use htaccess files or enable AllowOverride if you have access to the apache config files simply because it will use more resources to search the directory and find the htaccess files etc. Placing the changes into the httpd.conf file or conf.d/example_host.conf is much better.
One other note, mod_rewrite is over used and is really overkill for most purposes. I would advice you use mod_alias (see http://httpd.apache.org/docs/2.2/mod/mod_alias.html) instead. I should point out this can only be use in server configs or virtual hosts, so it will not work in a htaccess file. But it should be given preference should you have the choice between the two.
Alias /home /default.php
Alias /aboutme /about.php
Alias /work /work.php
AliasMatch /blog//([^/.]+)/? /blog/posts.php?val=$1
.. and so on.
Here is a good read on when not to use mod_rewrite:
http://httpd.apache.org/docs/2.2/rewrite/avoid.html
I am working with a custom MVC PHP framework and the index page (acting as a router) receives a GET variable "do" which contains the path that it will route to. If this variable is not set, it defaults to the Auth controller, method login.
require_once('config.php');
$controllerAction = isset($_GET['do'])?$_GET['do']:"auth/login";
require_once('core/main.php');
Then the index page (source code above) passes this $controllerAction to the main.php file, which autoloads the main controller and then loads the requested controller.
Thus, the URIs in this framework are of the form mysite.com/?do=controller/method/variable and I need it to be in the form mysite.com/controller/method/variable.
Here is the .htaccess file I tried to use, it just didn't work (I have other htaccess files working on the same server so it's not an Apache problem) :(
RewriteEngine On
RewriteRule ^([^/]*)$ /?do=$1 [L]
Someone suggested that I can do this using PHP but I am not sure how to go about that.
Edit:
The error is that I get "This page cannot be displayed", 404 errors, whenever I try to directly access the mysite.com/controller/method links rather than the default mysite.com?do=controller/method
Further Edit
(please note that other virtual hosts work fine on my localhost):
(XAMPP) Apache Virtual Hosting Info:
<VirtualHost *:80>
DocumentRoot "D:\sites\mysite.com\root\wwwroot"
ServerName mysite.com
ServerAlias mysite.com
<Directory "D:\sites\mysite.com\root\wwwroot">
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
File structure (Windows):
D:\
--sites
----mysite.com
--------#client_details
--------root
-----------#devfiles
-----------#vars_pwd
-----------wwwroot
--------------config
--------------core
--------------application
------------------controllers
------------------libraries
------------------models
------------------views
----------------------css
----------------------javascript
----------------------images
----------------------icons
First of all, there are some issues with your .htaccess contents. It's always a good idea to not rewrite if a file with the requested name exists. This allows you to have an img/ folder for your images or any other static content like css files, javascript, downloads, etc.. The first RewriteCond tells Apache to only rewrite if no folder with this name exists. The second one does the same with files. Then you probably want the QSA (i.e. Query String Append) option, which will pass all other GET variables to your script.
Under this conditions you can simplify the regex and use this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L,QSA]
You might be surprised because this is more or less the same as others posted. I use similar things for many of my projects and I've just tested it, I can guarantee that it works. There must be something wrong with your apache config.
When you have problems with mod_rewrite, the first thing you should try is to enable the module itself. Type these commands as root in your shell:
a2enmod rewrite
/etc/init.d/apache2 restart
The first one activates the module (or complains with Module rewrite already enabled if everything is ok) and the second one restarts your Apache server. The path may of course be different on your server.
Then you have to make sure that your VHost config allows you to use .htaccess files and do rewrites. This means AllowOverride must be set to at least FileInfo (or All). You could also try to put the rewrite rules right into the config file. Your config should look similar to this:
<VirtualHost *:*>
ServerName test.example.com
ServerAlias www.test.example.com
DocumentRoot /home/sites/test/
<Directory "/home/sites/test/">
Allow from all
AllowOverride All
Options +Indexes
</Directory>
</VirtualHost>
Note that you have to restart Apache if you change anything in there.
If that all doesn't help, it's always a good idea to have a look at the error logs. On my system they're located at /var/log/apache2/error.log (debian). They might give you more information on what's going wrong.
Try
RewriteEngine On
RewriteRule ^([^/]*)$ index.php?do=$1 [L]
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?do=$1 [L]
Check your apache logs, access logs specifically. If the folder is present in the web root, then you should be able to access it directly :). You might also want to check if you have duplicate virtualhost entries for the same site by chance.
This one is my customized MVC framework which is based on cake
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?do=$1 [QSA,L]
</IfModule>
May be this should help. The typical URL pattern for this site.com/controller/method
I don't know what your domain setup is like, but here are some suggestions.
If your code resides in the root of your folder, and the index file is called index.php try the following:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?do=$1 [L,QSA]
If your website exists in a subfolder e.g. www.example.com/site/, and the index file is index.php Then try the following (change /site/ to whatever your folder is).
RewriteEngine On
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site/index.php?do=$1 [L,QSA]
If you still get the 404 error message then do the following:
Make sure your site allows .htaccess files to be processed by checking AllowOverride is set to all. If you don't have access to the necessary config files to check, a simple test is to setup an .htaccess rule to redirect to a dummy file on your system. If it works, then your .htaccess is being executed fine.
Have a look at your MVC framework to see what page it's actually sending the request to. The problem may be that you haven't defined a handler for that particular request, and the default action of your MVC framework is to throw a 404 error.
Edit: Just reading your description, I notice you said that the URL should basically be something like mysite.com/?do=controller/method/variable. If it has be very strict about this format, then you'll also need to put in rules for removing any leading or trailing slashes, e.g. the following re-write rule should do it:
RewriteRule ^\?(.*)\?$ /index.php?do=$1 [L,QSA]
(This makes the leading and trailing slashes optional, but it should remove them from the actual value you pass to do).