.htaccess second RewriteRule note working - .htaccess

I have a PHP page like example.com/new/info.php?title=example. The .htaccess is in folder /new/. I tried it in the main directory also. My .htaccess looks like this:
ErrorDocument 404 /404.php
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ page.php?title=$1 [QSA,L,NC]
RewriteRule ^info/([a-zA-Z0-9_-]+)/$ info.php?title=$1 [QSA,L,NC]
The first Rule is working, but the second sends no GET['title'] to the server. The site info.php loads but without the variable. I have tested it on my localhost and it's working. I load it on my webspace and the second rule is not working.
I tried it also without new directory example.com/info.php?title=example and same not work.
What is my mistake?

It sounds like you have MultiViews enabled (possibly in the server config). You need to disable MultiViews for this to work. Add the following at the top of your .htaccess file:
Options -MultiViews
With MultiViews enabled, mod_negotiation will issue an internal subrequest for info.php when making a request for /info/example/ - before your mod_rewrite rule is processed, so no parameters are passed.

Related

.htaccess RewriteRule : not all rules work on Ionos [duplicate]

Start by explaining what I'm trying to do:
I've got different pages on my website. Some pages have the same templates so I create one page with parameters to adapt my page: Parameters are called pageview and lang the URL looks like this:
http://mywebsite/home/en <- http://mywebsite/index.php?pageview=home&lang=en
http://mywebsite/page2/fr <- http://mywebsite/index.php?pageview=page2&lang=fr
for example. To dot that, I use the famous .htaccess file and it module rewrite_module for Apache.
I've got also a contact page with a different template. It URL looks like this and here there is only one parameter:
http://mywebsite/contact/fr <- http://mywebsite/contact.php?lang=fr
http://mywebsite/contact/en <- http://mywebsite/contact.php?lang=en
Here is my .htaccess code:
RewriteEngine On
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
RewriteRule ^([a-zA-Z0-9]+)$ index.php?pageview=$1 [QSA]
RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)/?$ index.php?pageview=$1&lang=$2 [QSA]
The problem is that .htaccess file work for the index.php but not for contact.php
I can fully access to contact.php but the parameter is not detected
Thanks for your help 😀😀 !
EDIT
If I remove index parts to stay only the contact rewriteRule's the problem stay there.
contact.php and index.php are in the root folder
RewriteRule ^contact/([a-zA-Z0-9]+)/?$ contact.php?lang=$1
It looks like you may have a conflict with MultiViews. If MultiViews is enabled then mod_negotiation will rewrite a request for /contact/fr to /contact.php (without any parameters) before mod_rewrite is able to process the request.
Try disabling MultiViews at the top of your .htaccess file:
Options -MultiViews

apache $1 failed when file exist on public_html

i have a simple htaccess with a simple rewrite rule
AddDefaultCharset UTF-8
#
# page.php
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^page/([^.]+)?$ page.php?slug=$1 [L,NC,QSA]
I use this rule to show static pages for a long time.
The last few months this simple rule is not working in some servers (cPanel and VirtualMin) and don't understand where the problem is.
The rule make the match and execute page.php script which is located in public_html but the slug variable is null.
I have found that if i rename the page.php to _page.php its working fine.
To be more clear
if the bold matches i get no slug
page/([^.]+)?$ page.php?slug=$1
if i use
_page/([^.]+)?$ page.php?slug=$1
page/([^.]+)?$ _page.php?slug=$1
page/([^.]+)?$ page99.php?slug=$1
everything works fine.
Of course i have and other rules that are working fine.Only this rule is failed.
Any help appreciated
It looks like MultiViews is probably enabled on these servers where it is not working (ie. the slug URL parameter is missing).
Disable MultiViews at the top of your .htaccess file:
Options -MultiViews
MultiViews is disabled on a default Apache install, however, some shared hosts do enable this in the server config for some reason.
MultiViews basically enables extensionless URLs out of the box. When MultiViews is enabled and you request /page (or /page/<something>) then mod_negotiation looks for a resource that maps to the same basename (ie. page) that would return the appropriate mime-type (ie. text/html). In other words it would look for files of the form page.php or page.html etc. This occurs before mod_rewrite is able to process the request, so the URL parameter (in your mod_rewrite directive) is missing, because the RewriteRule did not get processed (it doesn't match).
I have found that if i rename the page.php to _page.php its working fine.
Yes, because the requested URL page does not map to the basename of a physical file.

.htaccess rewrite rule for same words

I have two rules causing problems:
RewriteRule ^posts$ posts.php
RewriteRule ^posts/create$ postscreate.php
I know that I could use "post/create" but these are just two rules out of more that are having all the same problem.
I worked with this rules perfectly on localhost (XAMPP).
However, once I moved the files to my hoster and visited "sub.domain.com/posts/create" it redirects me to posts.php instead of postscreate.php.
Why does it work fine on localhost but not on my hoster? And how can I fix it?
My full .htaccess looks like this (with the two rules only):
AddType application/x-httpd-php .html
RewriteBase /
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteRule ^(.*)$ https://sub.domain.com/$1 [R=301,L]
Options -Indexes
RewriteRule ^posts$ posts.php
RewriteRule ^posts/create$ postscreate.php
The .htaccess on localhost is exactly the same except the https rules. I also tried the .htaccess without the https rules on my hoster but it still didn't work.
Why does it work fine on localhost but not on my hoster? And how can I fix it?
It is most likely due to option MultiViews being turned on on hoster but turned off on localhost.
To disable this use this line on top of .htaccess:
Options -MultiViews
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.

Apache mod_rewrite not working

I have a problem with the mod_rewrite module for Apache 2.2
My code will neither work on localhost (Wamp on Win8 Pro, IPv6) nor on the webhotel (site5.com).
My goal is to generate SEO friendly URLs as:
www.xy.com/featured-artists.html
instead of:
www.xy.com/index.php?pageID=Artists
The PHP variable $pageID is defined on line 1 in my index.php:
<?php isset($_GET['pageID']) ? $pageID = $_GET['pageID'] : $pageID = 'Forside';?>
The code for the rewrite in my .htaccess file looks as follows:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
Unfortunately that doesn't have any effect on the site. The .htaccess file is working properly though, since other sections like
ErrorDocument 404 /index.php?pageID=404
work as expected.
No matter what I do - it won't work. So I really hope that some of you can help me with that.
Thanks!
Mod_rewrite doesn't seem to be turned on. Your rules are inclosed in a IfModule container:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/.]+)/?$ /index.php?page=$1
</IfModule>
So the rules aren't going to get executed. You need to turn on mod rewrite in server config (or your webhost) if you want any of your rules to work. Otherwise, it doesn't matter what you try.
Once you had mod_rewrite loaded, your rules should do close to what you want. It'll take /something and internally rewrite it to /index.php?page=somethin.

Url not complying to .htaccess rules

I have a problem with one of the url's in my website , it doesn't seem to comply to my .htaccess rules. The problem I'm experiencing is the following.
I access my site using url: http://www.website.com/sitemap
I have the following .htaccess rules:
ReWriteEngine On
php_value include_path ".:/Webserver/blabla/boo"
RewriteCond %{REQUEST_URI} !^/cms[A-Za-z0-9\-_/]*$
ReWriteRule ^([A-Za-z0-9\-_/]*)$ /site/index.php/
ReWriteRule ^$ /site/index.php/
So I would expect this URL call to rewrite to /site/index.php but the point is it never goes there and instead opens sitemap.xml which is in the webroot. It does work for all other URL's in my site. What could be the case here?
I found the culprit. It was due to the MultiViews option set in httpd.conf and each individual site .conf file. I disabled the MultiViews option in the per site .conf file setting it to -MultiViews
Maybe this question fits better in Serverfault. Is there a posibility for me to transfer it?

Resources