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

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]

Related

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>

How to change url php script htaccess methode

How to change this url
http://domain.com/search/song
to
http://domain.com/search/song.html
.htaccess File
<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?/$1 [QSA,L]
Try to add string
RewriteRule ^(/search/song)$ $1.html [L,R=301]
right after RewriteEngine On
If this doesn't work, try to put this:
RedirectMatch 301 ^(/search/song)$ $1.html
This should work perfectly.

How to stop redirection to index.php main file in code igniter?

I have a website domain.com which is based in code igniter. When someone enters a wrong url it redirects it to https://domain.com/index.php
I want to redirect redirect it https://domain.com and do not want index.php to be present in the urls. I have tried everything in htaccess file but it dooesnt work.
Here is my htaccess file
<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]
</IfModule>
AddHandler application/x-httpd-php55 .php55 .php
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,L]
If I understand rightly, you want to redirect index.php to the root?
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
Let me know if not

Adding 'public/index.php' to all requests without changing url

I want to add public/index.php to all request using .htaccess
I tried like this
RewriteEngine on
RewriteRule ^/(.*)$ /public/index.php/$1 [NC,L]
but it didn't changed ..
Example :
If i type
localhost/project/first
it should request
localhost/project/public/index.php/first
What is the issue in my .htaccess ?
You can use a rule like this in /project/.htaccess:
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php/$1 [L]
And this is /project/public/.htaccess:
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteBase /project/public/
RewriteRule index\.php$ - [L,NC]
# Redirect Trailing Slashes...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [L,R=301,NE]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
RewriteCond are required to avoid rewriting for real files and directories. And There shouldn't be any leading slash in RewriteRule pattern in .htaccess. .htaccess is per directory directive and Apache strips the current directory path (thus leading slash) from RewriteRule URI pattern.

Remove index.php from url Laravel 5

URLs are working fine in my application. I mean they are pretty URLs. Like http://www.example.com/
But it also works when you access the page with index.php like http://www.example.com/index.php, which I don't want because it is showing two links in sitemap for one page. One page without index.php and another with index.php. Demonstration of the sitemap is here https://www.xml-sitemaps.com/details-eln.6762418.html
Here is the .htaccess
<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]
</IfModule>
Put the following code :
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,7}\s/(.*)index\.php\sHTTP.*$
RewriteRule ^ /%1 [R=301,L]

Categories