this is my .htaccess-content:
RewriteEngine On
RewriteBase /
Options -Indexes -MultiViews
#Rewriting /profile.php?name=XY to /player/XY
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L]
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
If I am browsing to my-domain/player/XY it redirects me to player.php?name=XY (and prints an internal server error because player.php doesn't exist) instead of showing the profile.
But if I change it to
RewriteRule ^player/([^/]*)$ /profile.php?name=$1 [L] and open my-domain/playera/XY it works fine.
Can you help me, please?
Not sure why you're getting that error since the first rule should match /player/XY. But you can probably add a few conditions to your php rule to ensure that it is rewriting correctly:
#Remove .php file ending
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule (.*) $1.php [L]
Related
I attempted to create a little bit of htaccess which alters a URL from something like
http://localhost/website/page.php?id=_abc-123
to
http://localhost/website/page/_abc-123
It works for the most part, in that I can visit the page without having trouble locating scripts and CSS files. However, if I try to echo out $_GET["id"], instead of getting _abc-123, I will get _abc-123.php.
This is what I have so far within my htaccess file:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
All help is appreciated,
Thanks.
Test movie page first, and test if file with .php exists:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
# remove extensions
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
First of all, if you are not sure where your error lies, you can try online tools for .htaccess like htaccess.mwl.be.
Obviously your first RewriteRule contitions are met, which results in your "error".
With the help of this tool and some knowledge about how regex work, we can fix your .htaccess to this:
Options +FollowSymLinks
# remove extensions
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC]
# movie page
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^page/([^/]*)$ page.php?id=$1 [L]
The only thing I changed is removing the "L" flag from your first RewriteRule, because its RewriteCond is met but we need it to go through the second RewriteRule.
For more information about the L-flag have a look at the documentation.
I have a .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
And I have a file called random.php.
All I want to do is just call something.com/random, but Apache adds a trailing slash (using 301 Redirect) to the end of the URL, therefore giving an error stating that something.com/random/.php cannot be found.
EDIT 1: When I use
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [NC,L]
then my external files (.js, .css, etc.) can't load and the server responds with a 500 Internal Server Error response. Apache says that there were too many redirects.
Try this rule with optional trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
You almost had it right, you just needed to exclude the slash as well:
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.\/]+)$ $1.php [NC,L]
I have the following in my .htaccess:
Options +FollowSymLinks -MultiViews -Indexes
DirectorySlash Off
RewriteEngine On
RewriteBase /demotut/
# remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ /demotut/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)$ $1.php [L]
This works for me. The code is suppose to do the following:
1. if a request comes in at : 'host/demotut', then redirect to 'host/demotut/index.php'
2. if a request comes in at : 'host/demotut/somefile', then redirect to 'host/demotut/somefile.php'
3. if a request comes in at : 'host/demotut/somefile/and/urllike/vars', then redirect to 'host/demotut/somefile.php/and/urllike/vars'
The files in the root of the website act as controllers. As I said this actually works, but the repeating statements bother me.
I played with the [L], and commenting out the RewriteRule, but this is the only way I got it to work.
is there anyway to compact this?
I have been working on localhost, and my htaccess file is
Options +FollowSymLinks
Options -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) $1.php [L]
after adding the htacces code,the url
localhost/movies/news.php
works
localhost/movies/news
also works but
localhost/movies/news/
doesn't work. It shows "Internal Server Error".How to make it work with slash and without slash.
You an try this code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
# Internally forwards movies/news/ to movies/news.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
The problem is when you add the slash you have news/.php and this is not working.
A better solution is to rewrite to a GET variable something like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?url=$1 [L]
Then you can filter the GET variable in your script and include the file or content you need.
i want to rewite these in my .htacces file:
/*.php -> /*(/)
(e.g. gallery.php to /gallery or /gallery/)
/snippets.php*?s= -> /snippets/*
(e.g. snippets.php*?s=test to /snippets/test or /snippets/test/)
my code so far:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L]
RewriteRule ^snippets/([^/\.]+)/?$ snippets.php?s=$1 [L]
the bugs that appear using my code:
/snippets/ and /snippets/test(/) will alert an 500 Error. /snippets works fine.
what am i doing wrong?
Like Micheal said, you need to change the order, however Michael didn't move the RewriteCond's, which result in the unexpected behavior.
RewriteEngine on
RewriteBase /
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L]
I verified this code on my test server, just to be sure.
You almost have this correct. To take specific action for /snippets, it will need to come before the catch-all rule. Otherwise the first rule matches and routes to snippets/test.php which doesn't exist.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match snippets first...
RewriteRule ^snippets/([^/.]+)/?$ snippets.php?s=$1 [L]
# Then the catch-all for remaining matches
RewriteRule ^(.*)$ $1.php [L]