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.
Related
I have a URL like http://example.com/index.php/fruit/1a2b3c
I want to get the URI's for which I have written a code.
Now I want to remove the index.php from the visible URL, it should work even when the URL is http://example.com/fruit/1a2b3c and it should still point to index.php
I am on apache2 using PHP 7
Add following code to your .htaccess file.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*)/index\.php [NC]
RewriteRule ^ %1 [R=301,L]
change your .htaccess file to,
RewriteEngine on
RewriteCond $1 !^(index\.php)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
hope this will work.
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.
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]
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?
My .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([\w-]+)/?$ index.php?user=$1 [L]
RewriteRule ^([\w-]+)/([\w-]+)/?$ index.php?user=$1&type=$2 [L]
When I connect like dummy.com/UsernameTest it works perfectly fine, the problem is, when I connect with dummy.com/Username Test the URL changes the spaces to %20 (as expected?) and I receive the Object Not Found(404) error. I've already tried urldecode($_GET['user']); in my PHP file, but I had the same problem.
You need to change your regex to allow spaces and also use RewriteCond to prevent looping:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?user=$1&type=$2 [L,QSA]