mod_rewrite URI with same name as folder or files - apache

A simple .htaccess file with this:
RewriteEngine On
RewriteBase /
RewriteRule ^webshop$ /index.php?page=webshop [L]
... does not work for me because a have a file called webshop.php in the web root. Renaming the file solves the poblem, and changing the regex in the .htaccess file solves the problem, but still - it's only a partial match of the file name...? The only thing I can find on this is to use
DirectorySlash off
I've tried that and it made no difference.
Need some help here, there must be a pretty simple solution to this.

Most likely you have MultiViews options enabled. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
Disable it by placing this line on top of your .htaccess:
Options -MultiViews

Related

Apache .htaccess URL rewrite adding additional directory in URL path

I'm trying to build a URL rewrite by adding an additional directory in the URL path; for example, changing;
https://FQDN/mypage/ to https://FQDN/dev/mypage/
https://FQDN/template/ to https://FQDN/dev/template/
I have written the following .htaccess although this is having no affect. I have already enabled a2enmod rewrite.
RewriteEngine on
RewriteRule ^dev/(.+)$ /$1
Thank you #anubhava - simple fix, the rewrite was not activating due to AllowOverride None being active in the apache2.conf file. Changing this to AllowOverride All allowed the .htaccess file to be active.

url rewriting "double path for same file"

I have an issue with my htaccess file but I dont understand where the mistake comes from:
Ex:
I have the following file in my folder
contact.php
I did a basic rewrite like that
RewriteRule ^about/$ contact.php [L]
so now my contact page is accessible from http://localhost/project/about/
the problem is that page is also accessible from
http://localhost/project/contact/
Why is this happening?
How can I disallow that?
Thanks for all your answers!
That is due to enabling of option MultiViews. Option MultiViews is used by Apache's content negotiation module that runs before mod_rewrite and makes Apache server match extensions of files. So /file can be in URL but it will serve /file.php.
To disallow this you can add this line on top of your .htaccess:
Options -MultiViews

Static text on rewriterule

I've been trying different ways to create the url like this:
http://www.domain.com/postal-zip/123456
I've tried:
RewriteRule ^postal-zip/([0-9]+)$ code.php?thecode=$1 [L]
and
RewriteRule ^postal-zip/([0-9]+)$ postal-zip/code.php?thecode=$1 [L]
and a few more things but I have not got the result, does anyone have an idea about this?
Your rules look okay. It sounds like mod-rewrite is not turned on.
First, before your rules, make sure you have:
Options +FollowSymLinks -MultiViews
RewriteEngine On
Next, for this to work, mod_rewrite must both be installed and activated for .htaccess files. If you are not sure, to check if mod_rewrite is even installed, look at the list of installed modules in the output of phpinfo(); By default, mod_rewrite is not enabled for .htaccess files. If you are managing your own server, open httpd.conf and make sure that the webroot directory block contains one of these lines: AllowOverride FileInfo or AllowOverride All

How to enable apache rewrites step by step

I am writing scripts for Apache URL redirects.
I have researched the rewrite rules to be written.
Now I would like to know the procedure for implementing this.
Enabling mod_rewrites in http.conf
LoadModule rewrite_module modules/mod_rewrite.so
AddModule mod_rewrite.c
I have created .htaccess
Now I am not sure of the following.
1.Access rights required to do this.
2.The location to put .htaccess file
3.how to enable logs and write logs.
4.I have two web servers.Do I have to put this in both of them.
My rewrite rule looks some thing like this.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} old_domain\.com [NC]
RewriteRule ^(.*)$ http://www.new_domain.com/test$1 [L,R=301]
It would be good if some one help me with the step by step procedure to perform this.
1.Access rights required to do this.
You need write access to the folder the htaccess file is going to be uploaded to, and the htaccess file itself must have read rights by the webserver (most likely, the "apache" user)
Additionally, you need to have at the very least:
AllowOverride FileInfo
in the server config for the document root directory
2.The location to put .htaccess file
Based on the rules you have, you want it in the document root. It's denoted in the server config by the DocumentRoot directive
3.how to enable logs and write logs.
See: http://httpd.apache.org/docs/current/mod/mod_log_config.html
Specifically the TransferLog and ErrorLog directives
4.I have two web servers.Do I have to put this in both of them.
Do the same thing for each. If both webservers use the same document root, then obviously you don't need to put the htaccess file in 2 places. If they have different document roots, then put the htaccess file in both document roots.

Rewrite url rules in .htaccess inside a directory

I'm trying to use rewrite url rules using a .htaccess file. This seems to work fine in my main .htaccess file (only works for php pages which exist in same directory), but when I want to use another .htaccess for php file inside a subdirectory the rules don't apply at all and I get a 404 error page.
The file I want to point to is /news/story.php. story.php requests an integer variable called 'article' to fetch the article from database and display it.
So basically what I want to do is replace http://www.mydomain.com/news/story.php?article=1
with http://www.mydomain.com/news/story/article/1/
I'm using the following rules in the .htaccess file which is inside the 'news' directory.
# For security reasons, Option followsymlinks cannot be overridden.
#Options +FollowSymLinks
Options +SymLinksIfOwnerMatch
RewriteEngine on
RewriteRule story/article/(.*)/ story.php?article=$1
RewriteRule story/article/(.*) story.php?article=$1
Thanks for your help
RewriteRule story/article/(.*)/ news/story.php?article=$1
RewriteRule story/article/(.*) news/story.php?article=$1