htaccess file dont work with special character - php

i have my .htaccess copied from codeigniter ,
i have a page which it should have a href
<a href="/notification/<?= rawurlencode(urlencode($tab['href']))?>">
htaccess will get the request correctly if i remove the special characters from the href
the htaccess file:
# Disable directory browsing
Options All -Indexes
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Rewrite "www.example.com -> example.com"
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to the front controller, index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\s\S]*)$ index.php/$1 [L,NC,QSA]
# Ensure Authorization header is passed along
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
ServerSignature Off

With codeigniter you shouldn't be passing URL segments with special characters. Those filters are there for security reasons. Instead, pass them as GET variables.
<a href="/notification?tab=<?=rawurlencode(urlencode($tab['href']))?>">
.. and in your controller
class Notification extends CI_Controller {
public function __construct() {
parent::__construct();
}
public function index() {
$tab = $this->input->get('tab');
}

Related

php script won't run correctly in subdirectory coz of .htaccess

I can't run php script in subdirectory and i believe there's something wrong with .htaccess file.
my subfolder name is "crm".
and here's .htaccess code:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# option to force redirect to ssl (https) - uncomment the two lines below
# RewriteCond %{HTTPS} !=on
# RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.svg|\.png|\.jpg|\.gif|robots\.txt|\.png|\.ttf|\.woff)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Can you please help me to fix this?
And when I try to get an image address from the subdirectory it thinks the image is on the root

Having trouble understanding htaccess/mod_rewrite, apache isn't rewriting url as intended

I'm trying to redirect any request to my laravel project's index.php, located at localhost/test1/public/. here is my htaccess in the root folder:
Options -MultiViews
RewriteEngine On
RewriteBase /test1/public/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
RewriteRule ^()$ index.php?url=$1 [QSA,L]
To my understanding, anything after localhost/ will be put into the url variables. so for example. localhost/nothing should be rewritten to localhost/test1/public/index.php?url=nothing ... right?
when i directly type localhost/test1/public/index.php?url=nothing the page loads properly, but typing localhost/nothing currently gives me a 404 error. Is there any way i can check what URLs are being rewritten to?
Another assumption of mine that I'm not sure about is the .htaccess located in /test1/public will rewrite the url that was rewritten by the .htaccess file in root. is that correct? here is the laravel project's htaccess located in /test1/public.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>

.htaccess file issue while redirecting from index.php to normal link when on home page

I am working on a LARAVEL 5.3 application and trying to redirect all requests with index.php to without index.php
e.g.: if someone enters example.com/index.php/some-uri-segments,
it should redirect to example.com/some-uri-segments.
I have successfully achieved this with following code:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
#index.php to normal
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php(.*)$ $1 [R=301,L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
But there is a problem when the link is example.com/index.php.
The request with example.com/index.php is redirected to example.com/var/www/html/public
But I want it to be redirected to only example.com.
You are missing a / in your code. Try this one:
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^index.php(.*)$ /$1 [R=301,L]

URL shows /index.php in URL when viewing site through HTTP, but not when using HTTPS

Visiting any of the below links works fine, and there is no /index.php in the URL.
1) https://kdev.solutions
2) https://www.kdev.solutions
However, if you try to visit the site using HTTP, you will get redirected to the HTTPS version, but there will be /index.php in the URL now. How do I get rid of this?
1) http://kdev.solutions
2) http://www.kdev.solutions
Here is my .htaccess file:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
#get rid of index.php in home
RedirectMatch 301 ^/index.php/(.*)$ https://kdev.solutions/$1
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
</IfModule>
Remove this line:
RedirectMatch 301 ^/index.php/(.*)$ https://kdev.solutions/$1
This configs is enough for redirect non-https to https (you already have it):
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
You need to reorder your rules by keeping all redirect rules before internal rewrite rules:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301,NE]
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteCond %{REQUEST_URI} ^(.*/)index\.php$ [NC]
RewriteRule ^ %1 [L,R=301,NE]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Also make sure you clear your browser cache or test in a new browser.

How can I change the URL in the browser by htaccess?

I want to change my url by htaccess from
http://example.com/offer?search=something to http://example.com/offer/something
The following htaccess works from http://example.com/offer/something but if I typing the http://example.com/offer?search=something then the url is not changing to http://example.com/offer/something.
RewriteEngine On
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
Should I use "rewritecond"? What is the correct htaccess in this case?
I try make it in Laravel framwork. I have only one htaccess and this is the content, currently:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]
</IfModule>
UPDATE: Full .htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# redirect URL with GET parameter to pretty URL
RewriteCond %{THE_REQUEST} \s/+offer\?search=([^\s&]+) [NC]
RewriteRule ^ /offer/%1? [R=301,L]
# rewrite from pretty URL to actual handle
RewriteRule ^(offer/[^/]+)/?$ index.php/$1 [L,QSA]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
How it prevents looping: THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite rules, unline %{REQUEST_URI} which gets overwritten after other rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1
You also need to include a redirect from the url that contain a query string. I haven't tested this, but the first rule should catch the first URL and redirect it to the new format. The second rule should catch the new format and pass the correct values to the search parameter.
RewriteEngine On
RewriteCond %{QUERY_STRING} ^search=([-a-zA-Z0-9]+)
RewriteRule ^offer$ /offer/%1? [R=301,L]
RewriteRule ^offer/([^/]*)$ /offer?search=$1 [L]

Categories