Rewrite url using .htaccess in php not working - php

I want rewrite below url
http://www.example.com/page.php?n=About%2BUs&id=F534Z531G538
to
http://www.example.com/about-us
using .htaccess file.
i have tried below code in my .htaccess
Options +FollowSymlinks
RewriteEngine on
rewriterule ^page.php?n=About%2BUs&id=F534Z531G538(.*)$ http://www.example.com/about-us$1 [r=301,nc]
but this is not working and also id is randomly generate.
anyone can help me ?

try this
RewriteRule ^about-us/([a-zA-Z0-9]+)$ page.php?id=$1
for http://www.example.com/about-us/f14dfa
for http://www.example.com/about-us
RewriteRule ^about-us$ page.php

Related

Cant redirect my URL with htaccess

I am using WAMP and my url is localhost/hotel/hotels.php?id=21
Using rewrite rule as follow,
Options +FollowSymLinks
RewriteEngine on
RewriteRule hotels/id/(.*)/ hotels.php?id=$1
RewriteRule hotels/id/(.*) hotels.php?id=$1
But nothing happens..
My mod_rewrite is on and also changes done in httpd.conf file.
Please give me a suggestion to handle the issue?
You must use flags for your RewriteRule. You can change it as follow
RewriteEngine on
RewriteRule ^hotels/id/([\d]+)/?$ hotels.php?id=$1 [NC,L]
You can call your pages from
localhost/hotel/hotels/id/12
If your .htaccess file is located in localhost/hotel .

How to use htaccess code for URL rewrite in PHP?

I have a link like below:
In /country/search.php
<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">
When I use the below .htaccess code, it does nothing:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L]
It does in the tool:
http://example.com/0/0/4/122
... but when I click that link it shows old URL again.
What is the problem here?
I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/
Replace your code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]
# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
I think you misunderstood what the rewrite rules are trying to achieve in this context.
F.e. this rewrite rule
RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2
Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to
http://example.com/app.php?method=mymethod&arg=myarg
When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.
Original URL:
href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>
Ideal URL:
/India/andhra%20Pradesh
And .htaccess code:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

url rewriting with htacess not working

I have a static website upon which I have performed CMS but when I added .htaccess file for url rewriting its not working.
eg. www.example.in/new/index.php to www.example.in/new/index. there are about 50 pages in my website. what shall i do, i m completely lost. this is the first time i m using .htaccess file.
I have written:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?user=$1.
RewriteEngine On
RewriteRule ^new/([a-zA-Z0-9_-]+)$ new/index.php?user=$1
RewriteRule ^new/([a-zA-Z0-9_-]+)/$ new/index.php?user=$1.
You may need to set
AllowOverride All
in your Apache config.
http://httpd.apache.org/docs/2.0/howto/htaccess.html

.htaccess URL rewrite with one and two query string vars

I'm trying URL re-writing for the first time and I have the following URLS which need rewriting:
http://domain.com/resorts.php?countryname=Portugal
http://domain.com/rss.php?destination=Torremolinos&itemtype=rfamily
I've written the following in my .htaccess file:
Options +FollowSymLinks
RewriteEngine on
RewriteRule /countryname/(.*)/ resorts.php?countryname=$1
RewriteRule /destination/(.*)/itemtype/(.*)/ rss.php?destination=$1&itemtype=$2
But when I try the following, I keep getting URL not found. Please can someone point out what I may be doing wrong? Many thanks in advance.
http://domain.com/countryname/Portugal
http://domain.com/destination/Torremolinos/itemtype/rfamily
You can use this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule countryname/(.*)/? resorts.php?countryname=$1 [NC,L]
RewriteRule destination/(.*)/itemtype/(.*)/? rss.php?destination=$1&itemtype=$2 [NC,L]

shorten url problem using htaccess

I need to change
http://localhost/engineering/management/administrator/modules/course/view.php
to
http://localhost/engineering/course_view.php
using htaccess
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Works fine for this,
But for the below url its not working
http://localhost/engineering/management/user/modules/data/edit.php
Please try with this code:-
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.*)_(.*)\.php$ management/administrator/modules/$1/$2.php
RewriteRule ^(.*)_(.*)\.php$ management/user/modules/$1/$2.php
Hope it helps.
when you use this one!
RewriteRule (.*)_(.*)\.php management/administrator/modules/$1/$2.php
because it's possile ro rewrite other URLs(that you've mentioned), you can add special part to it to avoid such thing. e.g:
RewriteRule swd(.*)_(.*)\.php management/administrator/modules/$1/$2.php
and url:
http://localhost/engineering/swdcourse_view.php
and for second one, use other characters than swd!!
RewriteRule ^usr(.*)_(.*)\.php$ management/user/modules/$1/$2.php

Categories