URL Rewriting and GET values - php

I know there are lots of answer about this, but I can't understand why it does not work for me.
I want every URL like :
http://mywebsite.com/campagne/blabla
to be redirected to
http://mywebsite.com/campagne.php?c=blabla
Here is my .htaccess file:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule campagne/(.+)$ campagne.php?c=$1 [QSA,L] [L]
The URL is redirected to campagne.php but $_GET['c'] is not sent.
Can you help me? I cannot find what is wrong? It works on my local server but not online.

This is due to enabling of MultiViews option in your Apache. Place this line on top of your .htaccess to turn it off:
Options -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.

Related

why a rewrite_rule works only when the folder exist?

The objective is to input an url like
https://www.mywebsite/expert/188/name-of-the-expert
and return it to the server in the form
expert.php?exp=188
Like if the user typed in https://www.mywebsite/expert.php?exp=188
WHAT DOES NOT WORK:
simple rules like RewriteRule ^expert-([0-9]*)$ expert.php?exp=$1 [L,NC,QSA]
WHAT WORK
I have the following rewrite_rule that works only when I physically create the folder expert/ in my tree, i.e. /www/expert/
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp=$2 [L,NC,QSA]
Also, for this rule to work, I had to put the <base href="/"> in the page expert.php to avoid errors with all my linked resources:
Failed to load resource: the server responded with a status of 404 ()
The server is APACHE on a shared web hosting platform named OVH.
The full code of the issue:
<IfModule mod_rewrite.c>
RewriteEngine On
Options +FollowSymlinks
RewriteBase /
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^(.*)expert\/([0-9]*)(\/[a-z0-9\-\']*)?\/?$ expert.php?exp=$2 [L,NC,QSA]
</IfModule>
Have it like this with MultiViews turned off:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
# FRIENDLY URL FOR EXPERTS PROFILE
Rewriterule ^/?expert/(\d+)/?$ expert.php?exp=$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 changed from using rewrite rules to using Apaches FallbackResource after reading https://www.adayinthelifeof.nl/2012/01/21/apaches-fallbackresource-your-new-htaccess-command/. It's more of a 'if page not found, then run this page instead'
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
FallbackResource /root.php
</Directory>
The only thing I have found is that if the URL is for page that does exist - then it serves that instead of your base new base page 'root.php' in my case.

Rewrite not work properly

My problem is whenever I write
RewriteRule ^ab-c/([^?]) abc.php?test=$1
it works but when I write (no hyphen between b and c)
RewriteRule ^abc/([^?]) abc.php?test=$1
Rewrite Engine is not work properly.
This seems to be a problem of options 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 turn it off place this line at top of your .htaccess:
Options -MultiViews

Trouble with php mod_rewrite

i'm having some mod_rewrite problems. I am working on a rewrite simple rewrite tool that should help me with my website. but i don't know where is the problem
this is my .htaccess file:
RewriteEngine on
RewriteBase /
RewriteRule ^text\/$ text.php [L,NC]
and my phpinfo() says that i have my mod_rewrite loaded
also i have a apache hadler: AllowOverride On
so I don't know why this isn't working... can anyone tell me what else could be the problem ?
You need to add the following at the top of your .htaccess file
Options +FollowSymlinks
Also, what rewrites do is allow you to go to
http://www.mydomain.com/text
and see the content that would be generated if you actually went to
http://www.mydomain.com/text.php
rather than the other way around. So the rewrite rules are basically
RewriteRule <what-I-type-in-my-address-bar> <what-page-I-see>

mod_rewrite to serve a new version of existing files

I have just made a central dynamic script to replace a messy folder full of static files. However, I would like to preserve the old URL's without having to delete those files. Let me give you an example:
I have the following files:
/oldpages/projekt1/index.html
/oldpages/projekt2/index.html
/oldpages/projekt3/index.html
The actual new urls are:
/newscript/script.php?name=projekt1
/newscript/script.php?name=projekt2
/newscript/script.php?name=projekt3
The rewrite rule is:
Options -MultiViews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^oldpages/([A-Za-z0-9_\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]
However, whenever I try to access http://mydomain.com/oldpages/projekt3/index.html it just keeps giving back the old files. After googling, everyone said that multiviews may be the culprit, but apparently, that's not fixing it.
The rule works if the directory doesn't exist on the server, the following works perfectly:
Options -MultiViews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^newpages/([A-Za-z0-9_\-]+)/(index.html)?$ /newscript/script.php?name=$1 [L,QSA]
And http://mydomain.com/newpages/projekt3/index.html gets the right page.
What am I doing wrong?
Any help would be greatly appreciated.
If a .htaccess in /oldpages has RewriteEngine On, that overrides all rules in the .htaccess in /.

How do I stop URLs that don't exist from resolving to a file?

I don't know if this is an Apache or PHP thing but if you go to either of:
mysite.com/about
mysite.com/about/
it will show the mysite.com/about.php file instead of a 404 error, because the directory does not exist.
I am sure this is some setting, maybe an htaccess thing, any ideas?
As others have answered it might be a mod_rewrite rule; there is also a directive called MultiViews that is explicitly for enabling the functionality you describe.
http://httpd.apache.org/docs/2.2/content-negotiation.html#multiviews
The effect of MultiViews is as
follows: if the server receives a
request for /some/dir/foo, if
/some/dir has MultiViews enabled, and
/some/dir/foo does not exist, then the
server reads the directory looking for
files named foo.*, and effectively
fakes up a type map which names all
those files, assigning them the same
media types and content-encodings it
would have if the client had asked for
one of them by name. It then chooses
the best match to the client's
requirements.
Look for
Options MultiViews
in your httpd.conf or virtualhost-specific config file.
That's not default behavior, for sure, so it's hard to say. If that were happening on the Apache side, look for a sequence of rewrite matching instructions; they'll likely begin with RewriteCond and ending with RewriteRule.
If you don't see anything like that in your VirtualHost config or site .htaccess, it's being managed by whatever PHP framework you're using, and you'll need to configure things there instead.
with this .htaccess you can parse, with $_SERVER[REQUEST_URI] in the PHP side, the complete URL and then perform your desired action, this is a general way, you should consider error controlling and 404 redirection:
ErrorDocument 404 /404.php
<IfModule mod_rewrite.c>
RewriteEngine On
# RewriteBase
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
and int the PHP side, that should be this index.php
$URLarray = explode('/', $_SERVER['REQUEST_URI']);

Categories