url rewrite with htaccess - php

i want rewrite my url
this is my index.html page
Main <br>
About <br>
Contact <br>
this is my .htacees file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/([a-z])/([0-9])?$ index.php?url=$1&page=$2 [L]
i want change my url to
http://localhost/htac/index/about/2 (http://localhost/htac/index/{url}/{page})
but url is :
http://localhost/htac/index.php?url=about&page=2

Have your /htac/.htaccess like this:
RewriteEngine on
RewriteBase /htac/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?url=([^\s&]+)&page=([^\s&]+) [NC]
RewriteRule ^ %1/%2? [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ index.php?url=$1&page=$2 [L,QSA]
You need to change source of your HTML to have links as: Main

Related

.htaccess: Rewrite dynamic url with static url

I want to enter http://localhost:81/admin/dashboard in my browser but the request should be http://localhost:81/admin/index.php?page=dashboard.
The mod_rewrite is enabled and i tried this in the .htaccess but it didn't work. The .htaccess is located in htdocs/admin/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?page=$1 [NC]
You can match the trailing slash optionally by adding a ? next to it in the pattern :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [NC]

301 Redirect dynamic url to a rewrited url

I want to redirect all dynamic links to redirect to its rewritten url eg:
http://www.example.in/ads-detail.php?location=Mumbai&id=37&name=sudha-restaurant
to
http://www.example.in/Mumbai/37/sudha-restaurant
My htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /ads-detail.php?location=$1&id=$2&name=$3 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /ads-detail.php?id=$1&name=$2 [QSA,L] –
Tried alot i haven't got any solution please help , Thanks in advance
Try this in your htaccess :
RewriteEngine on
#1) redirect "/ads-detail.php?location=foo&id=123&name=bar" to "/foo/123/bar"
RewriteCond %{THE_REQUEST} /ads-detail.php\?location=([^&]+)&id=([^&]+)&name=([^\s]+) [NC]
RewriteRule ^ /%1/%2/%3? [NC,L,R]
#2) if the requested is not for an existing directory
RewriteCond %{REQUEST_FILENAME} !-d
#3) then rewrite "/foo/123/bar" to "ads-details.php"
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /ads-detail.php?location=$1&id=$2&name=$3 [NC,L]

controller and method translate in htaccess mod rewrite

I have some URL:
index.php?controller=user&method=show&id=7
in this URL: controller,method and show, are my keys
user, show and 7 are some kind of value
I would like translate URL to:
index.php?user/show/7
Is there any posibilities to translate this URL in .htaccess file?
Now, my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /Cobbler/index.php
I don't see the benefit of this but you can replace your current code by this one
RewriteEngine on
RewriteBase /Cobbler/
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule . ./ [R=301,L,QSA]
RewriteCond %{QUERY_STRING} ^([^/]+)/([^/]+)/([^/]+)$
RewriteRule ^$ index.php?controller=%1&method=%2&id=%3 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]

url rewriting while url creates dynamically

I had rewrite a URL from example.com/service.php?p_id=1&s_id=seo to example.com/1/seo/.
By The following rule:
this rule is for example.com/services.php?p_id=1&s_id=seo
RewriteEngine On
RewriteCond %{THE_REQUEST} /services\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ services.php?p_id=$1&s_id=$2 [QSA,L]
Now the problem is that when i use the following rule for another url like example.com/about-us.php?p_id=1&s_id=2 and for example.com/it-training.php?p_id=1&s_id=2 then the url redirects as example.com/1/2/index.php but it should be as example.com/1/2 ,
formated rule for about-us.php is given bellow
RewriteEngine On
RewriteCond %{THE_REQUEST} /about-us\.php\?p_id=([^&\s]+)&s_id=([^&\s]+) [NC]
RewriteRule ^ /%1/%2/? [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ about-us.php?p_id=$1&s_id=$2 [QSA,L]
In other case i have some url like example.com/packages.php, index.php, i also want to remove the .php from that kind of files

Rewrite url in htaccess php

I want to rewrite url as following.
This is url : http://www.xyzdomain.com/?page=9
i want it like this : http://www.xyzdomain.com/9
Add this to the htaccess file in your document root:
RewriteEngine On
RewriteCond %{THE_REQUEST} \ /+\?page=([0-9]+)
RewriteRule ^ /%1? [L,R]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([0-9]+)$ /?page=$1 [L,QSA]

Categories