here is my code in .htaccess
<Directory "/">
AllowOverride All
</Directory>
RewriteEngine on
RewriteRule ^custom contactus.php[NC L]
Remote site path or my all website files contains in /www
current url : example.com/contactus.php
expected output url: example.com/custom
Try the following rule.
RewriteEngine On
RewriteRule ^custom$ /contactus.php [L]
Invalid htaccess file
A Directory directive is invalid in a .htaccess file:
Description: Enclose a group of directives that apply only to the
named file-system directory, sub-directories, and their contents
Context: server config, virtual host
Note that .htaccess is not a valid context. Similarly, AllowOverride is also invalid in a .htaccess file:
Description: Types of directives that are allowed in .htaccess files
Context: directory
Were it valid in a .htaccess file, it would defeat the purpose of what the directive is for.
Valid example
The entire contents of the .htaccess file to do what's in the question are:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^custom contactus.php [NC,L]
</IfModule>
I.e. If mod rewrite is enabled, apply that rewrite rule. Note that the syntax for rewrite flags is a comma separated list.
Related
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
So I have a server running on Digital Ocean. I'm trying to run Anchor CMS on it. So this is what the URL should look like http://colourity.com/posts/hello-world. But that doesn't seem to work. When I try http://colourity.com/index.php/posts/hello-world, all works fine. But the problem here is that I'm using .htaccess to correct this, here's what I have,
Options -indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
That should of gotten rid of the index.php, additionally mod_rewrite is enabled. I've also restarted the server to see if that makes a difference, but it hasn't. Any ideas?
AllowOverride All must be included in the Apache configuration so that .htaccess rules can overwrite your configuration. From the documentation:
When the server finds an .htaccess file (as specified by AccessFileName) it needs to know which directives declared in that file can override earlier configuration directives.
When this directive is set to None, then .htaccess files are completely ignored. In this case, the server will not even attempt to read .htaccess files in the filesystem.
When this directive is set to All, then any directive which has the .htaccess Context is allowed in .htaccess files.
It's default value is All, but some template configuration files may have it manually set as None. Also note, that this syntax is only available in <Directory /> sections.
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 have a website, lets say: http://www.example.com/
I'm using rewrite module, but I have a subfolder forum.example.com
I don't want the .htaccess to affect this directory, how do I do it?
If your forum.domain.com points to a subdirectory like you suggested (and your current .htaccess file is in the directory that contains that subdirectory, or some directory above it), then all you have to do is add a .htaccess file in the subdirectory with the following:
RewriteEngine Off
And it will disable mod_rewrite for any requests that point to that directory.
In httpd.conf:
<Directory /relevant/directory>
AllowOverride None
</Directory>
See the documentation.
It is probably easiest to precede the RewriteRule with a RewriteCond directive
RewriteCond %{HTTP_HOST} ^www.domain.com$
This way, the RewriteRule is only applied to requests matching the RewriteCond, which required the HTTP host header to match www.domain.com.
(assuming Apache 2.2; I'm not sure whether this syntax is different for previous versions).
Set AllowOverride directive in the subdomain virtualhost to None
You could use a RewriteCond to only apply your rule when a particular domain name is used.
RewriteCond %{HTTP_HOST} ^www\.domain\.comt$ [nocase]
RewriteRule foo bar [l]