.htaccess (Object not found when URL has spaces) - php

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]

Related

Get URI params without index.php

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.

Weird .htaccess behaviour

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]

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

URL doesn't work with slash after removing extension using htaccess

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.

htaccess remove .php and keep query string

Here is my .htaccess file right now.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [NC,L,QSA]
This works in the fact that it makes my pages accessible when not using the .php extension.
Old = domain.com/test.php
New = domain.com/test
The bad thing is that when I send get data with the following link the data is not passed. I thought the QSA option did that, whats the deal?
domain.com/test?id=1
Matching the entire query string and appending it to your new URL using a back-reference should work.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
Here's a more simple solution. We use it on our team project. It will work not only in the root, but in any directory of your website.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
If you are passing id then you have to use this code
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^test/(.*)$ /test.php?id=$1
now you can access
domain.com/test?id=1
from
domain.com/test/1

Categories