mod_rewrite [PT] flag returns bad request - .htaccess

Initially, this code was written to create "pretty" urls.
When using the code below, mod_rewrite works as it should.
<Directory /var/www/vhosts/myurl.com/httpdocs>
Options Indexes FollowSymLinks
php_admin_flag engine on
php_admin_value open_basedir none
AllowOverride all
Order allow,deny
Allow from all
</Directory>
RewriteEngine on
RewriteCond %{HTTPS} !=off
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteCond $1 ^(register|account|logout|profile|edit_profile).*$
RewriteRule ^/(.*)$ https://myurl.com/?get=$1
It takes any matching url such as https://myurl.com/register and rewrites it as https://myurl.com/?get=register. The appropriate page is found and displayed in the browser.
However, I want the original url to be passed through to the browser. To achieve this, I added the [PT] Flag to my RewriteRule as shown below:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [PT]
This lets https://myurl.com/register (same url as above) through to the browser but no longer displays the page. Instead, it returns the following error:
Bad Request
Your browser sent a request that this server could not understand.
Client sent malformed Host header
Relevant Info:
OS: Linux
Server: Apache
Control: Plesk
Directory: /var/www/vhosts/myurl.com/conf
File: vhost_ssl.conf
I've searched multiple forums and articles to no avail.
Article 1: Making prettier URLs with mod_rewrite (includes use of [PT] Flag)
Does anyone have ideas on what's going on here and how to fix it? How can I get the "pretty" url to display?

So after two days of frustration, I finally solved the puzzle.
First I experimented with the RewriteRule from by changing it from:
RewriteRule ^/(.*)$ https://myurl.com/?get=$1 [P]
to
RewriteRule ^/(.*)$ https://myurl.com/index.php?get=$1
without any flags.
This produced an infinite loop whenever the rewrite was triggered. But I noticed that the loop was happening without redirecting the url (maintaining the 'pretty url'). Progress!
I decided to simplify the code. I removed 'LA-U:' from the RewriteCond as shown below:
RewriteCond %{LA-U:REQUEST_FILENAME} !-f
RewriteCond %{LA-U:REQUEST_FILENAME} !-d
to
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
That did the trick! Thanks to those that commented. Hope this helps someone in the future.

Related

Exclude php from url in get request

Is it possible to exclude the .php from URL?
Currently i got this in my .htaccess to remove php from urls:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
But whenever i try to do
dashboard?test=lol
It doesn't work, but if i do
dashboard.php?test=lol
then it does work.
I've seen other sites being able to do it without the .php in url... so how?
Thanks.
Have you restarted the webserver?
You also need to enable AllowOverride in Apache configuration. Something similar to this:
AllowOverride All
With your shown samples, try following. Please make sure to clear your browser cache before testing of URLs.
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php\?(\S+)\s [NC]
RewriteRule ^ /%1?%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/?$ $1.php [QSA,L]
Just add this line in your site root .htaccess to enable content negotiation:
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.

Is it possible to use htaccess to rewrite URLs without file extensions with MultiViews on

I'm using the following .htaccess file in order to internally rewrite URLs without the file extension, i.e. /mypage opens the content of /mypage.php or /mypage.html
Options -MultiViews
# start rewrite engine
RewriteEngine on
# internally rewrite "page" to "page.html"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
# internally rewrite "page" to "page.php"
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
It works exactly is it should on my local WAMP server.
However on my shared hosting service it gives an Internal server Error (500).
My hosting provider told me that the error log says that "Option MultiViews not allowed here" and confirmed me that MultiViews is enabled by default.
If I comment the first line there is no Error 500 but the rewriting doesn't work anymore (because MultiViews is enabled). They said to me that they can not change it for my host.
So is it possible for me to get this .htaccess rewriting URL working with MultiViews on ?

.htaccess Rewrite Rules unexpected behavior

I'm running a few .htaccess rewrite rules, to transform the URL path into a index.php query parameter.
So basically when someone visits https://example.com/elections it transforms the URL into https://example.com/?page=elections
Options -Indexes
RewriteEngine on
# Ignore extensions
RewriteCond %{REQUEST_URI} !\.(css|js|jpg|png|gif|svg|pkg)$ [NC]
RewriteRule ^([^/]+/[^/]+/). $1 [R=301,L]
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)$ /index.php?page=$1 [L,QSA]
This works great! There's only one issue, and that is unexpected behavior when there are 3 or more slashes in the URL the visitor entered.
So when someone visits https://example.com/one/two/three it transforms the URL into https://example.com/C:/xampp/htdocs/project/test/test/ (localhost test).
How can I make it so it just returns the regular 404 instead of this unexpected behavior?
(It's odd because it doesn't do that with 2 slashes)
Converting my comment to answer so that solution is easy to find for future visitors.
Problem is due to your first 301 error which causing this unwanted redirect and that too to a wrong target URL.
You can just keep this code in your .htaccess:
Options -Indexes
RewriteEngine on
# Rewrite
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9]+)/?$ index.php?page=$1 [L,QSA]
Make sure to test after clearing browser cache or test in a new browser to avoid old browser cache.

mod_rewrite rule to match exact URL only

I'm having an issue with mod_rewrite where I want to match—and replace—a specific URL. The URL I want to rewrite is:
http://example.com/rss to http://example.com/rss.php
That means, if some one were to append anything after rss a 404 Not Found response be sent. Currently I'm using this mod_rewrite snippet:
Options -Indexes
RewriteEngine on
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+) index.php/$1
But this matches rss and rss with anything else added to the end. How can I re-write the above to acces only http://example.com/rss as the pattern for mod_rewrite to match against?
You are getting this error because /rss is being redirected twice in your rules by both RewriteRules. Have your rules like this:
Options +FollowSymlinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
# pick up request for RSS feed
RewriteRule ^rss/?$ /rss.php [L,NC]
# pass any other request through CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (?!^rss\.php$)^(.*)$ /index.php/$1 [L,NC]
So with above rules it will redirect /rss OR rss/ URIs to /rss.php however /rss/foo will be redirected to /index.php since your 2nd rule is forwarding everything to /index.php
I was suprised to see that your rules just don't work, because in my first attempt I would have come to a very similar solution. But looking at the rewrite log revealed the real issue.
As discribed here the server prefers real files over directories. So internally rss/something becomes rss.php/something when applying the rewrite rules and things get weird.
So, one solution is to check if the Option MultiViews is enabled for the web directory either in .htaccess or in the vhost configuration. If so, remove it - which is what worked for me in this example.
If you need MultiViews, then I guess the only chance is to rename rss.php to rss-content.php and change the rule accordingly.
One additional note: you might want to add the following line after the # ... CMS block to prevent endless recursive calls.
RewriteRule ^index\.php/.* - [PT,L]
I hope this solves your rewrite problem.

Godaddy: Rewrite with .htaccess

I have this htaccess setup on my clients site, they use godaddy
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
it worked fine on my MAMP but does not seem to work on the server
they have setup a subdomain like demo.example.com and the htaccess exists in the demo folder but I get 404 trying to get to page like demo.example.com/test but demo.example.com/test.php works fine so this tells me the htaccess is not work. does anyone know what I need to do to make this work ?
Try:
1) Add this line into your .htaccess: Options +FollowSymlinks
2) Add leading slash before $1, e.g. RewriteRule ^(.*)$ /$1.php [L,QSA]
P.S. There are some "rumours" that GoDaddy may not pick up changes in .htaccess file straight away.
I would also suggest to define custom 404 error handler (simple PHP script) that may help with debugging this (put this line into your .htaccess):
ErrorDocument 404 /404.php
404.php (as simple as this)
<?php
Echo "404\n";
phpinfo(INFO_VARIABLES);
The details there may help identify the issue.
UPDATE:
We have a domain name & hosting that we do not use which is about to expire. So I have logged in and tested these rules myself. Got it working straight away. Here is the setup I have:
Apache/1.3.33
Hosting Configuration: 2.1
PHP 5.x (did not bothered to check exact version)
Data Center: US Regional
.htaccess was already present and had only 1 line 9possibly left over from the site that was there originally):
addhandler x-httpd-php5 .phtml
These lines were added to .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
I have created simple php file to check if it works. Called it echo.php. Then I have tried these URLs:
http://www.example.com/abcd -- saw error page (as expected)
http://www.example.com/echo -- saw my page -- echo.php (as expected)
The full physical path to the echo.php is: /home/content/b/u/y/buyxxxxxxx/html/echo.php; ftp/website root is /home/content/b/u/y/buyxxxxxxx/html/
Try this simple setup. DO not do anything complex until you solve this issue.
This is right way of play with .htaccess on godaddy
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L,QSA]
This answer on Stack Overflow might help you aswell:
Add this:
Options +FollowSymLinks -MultiViews -Indexes
at the very top and give it a try.

Resources