Rewrite query string with htaccess - php

I need to rewrite this url
category.php?cat=computers
to
category/computers
Or, is it the other way round?
How do I do that with .htaccess?

The rule is:
RewriteRule ^category/(.+)/?$ category.php?cat=$1 [NC,L]
your .htaccess should look like this:
<IfModule mod_rewrite.c>
#if the name of the php file is the same of the path, you have to remove MultiViews
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteRule ^category/(.+)/?$ category.php?cat=$1 [NC,L]
</IfModule>

Related

Rewritting URL by htaccess

When i click on a link it should redirect to
for ex; www.domain.com/demo/item.php?item=abc&id=3
but in url it should show as www.domain.com/demo/abc/3
im Using
<IfModule mod_rewrite.c>
Options +SymlinksIfOwnerMatches
RewriteEngine on
RewriteRule ^demo/(.+) demo/item.php?item=$1&id=$2 [NC,L]
Please help
You can try with this one, it will not redirect your URL but if you execute your URL (www.domain.com/demo/abc/3) it will be work.
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^demo/([0-9a-zA-Z\s_]+)/([0-9]+)(/?)$ demo/item.php?item=$1&id=$2 [NC,L]
You could try like this after adding the rewritebase directive
RewriteEngine on
RewriteBase /
RewriteRule ^demo/([0-9a-zA-Z\s_]+)/([0-9]+)(/?)$ demo/item.php?item=$1&id=$2 [NC,L]

How to change url like .php?name=neco to url /neco in .htaccess?

Ive been trying this commands, but with no luck :
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^product/(.*+) product.php?name=$1
Make sure your mod_rewrite rules are inside an IfModule:
<IfModule mod_rewrite.c>
#...
</IfModule>
The rule you have will reroute example.com/product/foo to example.com/product.php?name=foo.
If you want to route example.com/foo to example.com/bar.php?name=foo you can do this:
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^([^/]*)$ /bar.php?name=$1 [L]
</IfModule>
so it will looks like this?
Options +FollowSymLinks
RewriteEngine on
<IfModule mod_rewrite.c>
RewriteBase /
RewriteRule ^([A-Za-z0-9]+)$ product.php?name=$1 [L]
</IfModule>
Ive been trying lot of Rewrite Rules, but nothin happens the script and .htaccess are in same directory of course

.htaccess URL Rewrite if a parameter is not exist

Below is the code which i use to url rewrite in our .htaccess file
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
from the above code if the url is www.example.com/pages/2 then actually it goes to url www.example.com/page/index.php?name=2
but also i want something like this : if a page request is www.example.com/pages/2/user/rahul then it should also go to same url www.example.com/page/index.php?name=2&user=rahul
so doing the above thing i did something like this :
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule pages/(.*)/? /page/index.php?name=$1 [NC,L]
RewriteRule pages/(.*)/user/(.*)/? /page/index.php?name=$1&user=$2 [NC,L]
but it does't work how i want.
Try :
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule ^pages/([^/]+)/user/([^/]+)/?$ /page/index.php?name=$1&user=$2 [NC,L]
RewriteRule ^pages/([^/]+)/?$ /page/index.php?name=$1 [NC,L]

RewriteEngine to hide extension ?lang="xx"

How can I rewrite this URL using .htaccess?
or just hide ?lang=es.
http://website/services?lang=es
to:
http://website/services/es
Try this in a .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^services/([a-zA-Z]{2})/?$ /services?lang=$1 [QSA,L]
</IfModule>

Using .htaccess to redirect to relative address

I am very new to URL rewriting using .htaccess and I would like to redirect anyone entering the domain below
http://domain.tld/admin
to be redirected to
http://domain.tld/index.php?section=admin
but without including the full address. This is the code I am using but it doesnt seem to work
Options -Indexes
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^admin /index.php?section=admin
</IfModule>
Where Im I going wrong?
Thanks
You can use this more correct regex rule with L flag:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^admin/? index.php?section=admin [L,QSA,NC]

Categories