RewriteRule working on http but breaks on https - php

I have some RewriteRule in htaccess to have some pretty urls for my landing page. The url can look like www.site.com/new-document and my RewriteRule will make that www.site.com/index.php?page=new-document
This way I can leave the pretty url and use index.php to change the text depending on the page variable.
Everything works perfectly well on http but once I add https to the url I get a Not Found error as in the RewriteRule is not triggering.
Here is a copy of my htaccess code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^new-document$ index.php?page=new-document
RewriteRule ^old-document$ index.php?page=old-document
</IfModule>

Grasshopper and anubhava pointed me in the right direction!
My problem was that the https virtual host did not have the AllowOverride All directive.
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>

Can you get to the HTTPS URL without the RewriteRule? It sounds like Apache may be using a different virtual host or not accepting HTTPS at all.

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.

URL Rewriting and GET values

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.

How to make an alias in .htaccess to change a local URL

I'm pretty sure this is a duplicate but I just don't know how to find the answer to this simple problem.
So I have a local page like this:
http://example.com/mypage.php
And all I want to do is to have anyone who tries to access http://example.com/subfolder/mypage.php to actually load the page at the above location.
I don't want a redirect. http://example.com/subfolder/mypage.php does not exist as an actual path on my server, but I want to make it available via rule in .htaccess
Try this:
RewriteEngine on
RewriteRule ^subfolder/(.*) /$1
This rule rewrites every request that starts with /subfolder/ to /. Or if you need only for a specific URL-path:
RewriteEngine on
RewriteRule ^subfolder/mypage.php /mypage.php
You can add it in .htaccess
RewriteEngine On
RewriteRule ^([^/]+)/(.*) /$2
or use directly in httpd.conf like
<Directory /var/www/>
Options Indexes FollowSymLinks ExecCGI
AllowOverride None
Order deny,allow
Allow from all
RewriteEngine On
RewriteRule ^([^/]+)/(.*) /$2
</Directory>
use $1 to get subfolder and $2 to get mypage.php :)

php simple mod_rewrite issue on Snow Leopard

I have the following within my htaccess page:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*?)$
index.php?page=$1
</IfModule>
but this does not work and gives me the Internal error (500).
but if I comment out the last two lines the page loads ok not errors but fails to d what I want it to.
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine on
#RewriteRule (.*?)$
#index.php?page=$1
</IfModule>
Any ideas what this could be??
Mac Snow Leopard.
My directory is within DocumentRoot "/Library/WebServer/Documents" and I do not use the normal http://localhost/~User/ but http://localhost/?? etc
Thanks
UPDATE:
I have come back to this as sidelined on other things for ages. So please find my new details below, with these details i get the following error still:
Internal Server Error
The server encountered an internal error or misconfiguration and was unable to complete your request.
Please contact the server administrator, you#example.com and inform them of the time the error occurred, and anything you might have done that may have caused the error.
More information about this error may be available in the server error log.
my url:
localhost/exp/index.php
My htaccess:
RewriteEngine On
RewriteRule (.*?)/(.*?)/(.*?)$
exp/index.php?page=$1&action=$2&id=$3
No wif I comment out all except the 'RewriteEngine On'
RewriteEngine On
#RewriteRule (.*?)/(.*?)/(.*?)$
#exp/index.php?page=$1&action=$2&id=$3
I get the 403 forbidden message. I fi comment them all out the page loads.
my /etc/apache2/httpd.conf file details are below:
<Directory />
Options Indexes MultiViews FollowSymlinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "/Library/WebServer/Documents/">
Options Indexes MultiViews
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I use this as my localhost and not my user one localhost/~User/
So to the life of me do not knwo whats going on.
# Turn on URL rewriting
RewriteEngine On
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php?page=URL
RewriteRule .* index.php?page=$0 [PT]
the answer came from here:
Answer to my question here
RewriteEngine On
RewriteRule ^(.*?)/(.*?)/(.*?)$ index.php?page=$1&action=$2&id=$3
seems to be the break and it needing to be on one line?

URL rewriting in PHP without htaccess

Website is running on a web host where we don't have access to a .htaccess file. However, I want to do URL rewriting for user friendly URLs.
e.g. Original URL
www.example.com/file?q=name
expected URL
www.example.com/file/name
As other people said, just use links like /index.php/nice/looking/url.
The "index.php" in the middle of the URL might look a little strange, but I don't think it's possible to have it look better without .htaccess
Else, you could ask your hoster to redirect any URL to /index.php so that you can handle URL rewriting without having /index.php in your URL.
Then you can just use a regex match to detect what file to include.
preg_match('#[/]{1}([a-zA-Z0-9]+)#', $_SERVER["PATH_INFO"], $matches) ($matches will contain all "parts" of the url in an array)
Be careful with including the files, use a whitelist so you're sure nobody would be able to load internal files.
as Alix Axel suggested you can use
www.example.com/index.php/file/name
then you will use $_SERVER['REQUEST_URI'] to process the URL.
Your best bet will be to have URLs such as this:
www.example.com/index.php/file/name
You'll to rewrite your PHP code though.
If you have an Apache server and AcceptPathInfo is enabled, then you can use the URL you wrote. A request of /file/name will then be automatically rewritten to /file with the PATH_INFO value of /name if /file is a regular file.
This is possible solely using language/content negotiation in the Apache configuration (httpd.conf or apache2.conf) along with rewrite rules. Use of an .htaccess file is not required.
In apache2.conf file (or httpd.conf) add the Multiviews directive, MultiviewsMatch directive, and rewrite rules:
<Directory /var/www/html>
Options Indexes FollowSymLinks Multiviews
AllowOverride None
Require all granted
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
<Files "*.php">
MultiviewsMatch Any
</Files>
</Directory>
Then save the file and restart Apache sudo systemctl restart apache2
If you specify the Options directive later in httpd.conf, you have to repeat the Multiviews directive for each site:
<Directory /var/www/html/chocolate.com/>
Options FollowSymLinks Multiviews
AllowOverride All
Require all granted
</Directory>
<Directory /var/www/html/vanilla.com/>
Options FollowSymLinks Multiviews
AllowOverride All
Require all granted
</Directory>
This will not adversely affect content negotiation if you are already serving in multiple languages. For example, replacing example.com/home.php with
home.en.php
home.fr.php
home.de.php
Then in apache2.conf:
AddLanguage fr .fr
AddLanguage de .de
AddLanguage en .en
LanguagePriority fr de en
ForceLanguagePriority Fallback
Your language will still be negotiated but the php extension will not appear in the URL.
or can create a routing switch in the index.php file:
$request = $_SERVER['REQUEST_URI'];
switch ($request) {
case '/file' :
require __DIR__ . '/file';
break;
case '' :
require __DIR__ . '/file';
break;
case '/file/name' :
require __DIR__ . '/file?q=name';
break;
default:
http_response_code(404);
require __DIR__ . '/404.php';
break;
}
Try this
www.example.com/file?q=name
to
www.example.com/name (better than www.example.com/file/name)
Open your .htaccess from your project root folder and add this code
Options +FollowSymLinks
RewriteEngine On
RewriteRule \.(js|css|jpe?g|png|gif)$ - [L]
RewriteRule "^([ \w-]+)/?$" /file?q=$1 [L,QSA]
This solves your problem. See the post how to rewrite URL in .htaccess

Categories