rewrite .php extension - php

Please how can I rewrite
Could anybody please rewrite this url?
http://localhost/display_news_cat.php?news_cat_id=14&p=2
to
http://localhost/display_news_cat/14/2
Thank you

You can do thtat with a .htaccess file
put this in a .htaccess file and place it in de root of your site
apache must have mod-rewrite on
RewriteEngine on
RewriteRule ^news.php news [QSA,L]
try this for all files
RewriteEngine on
RewriteRule ^(a-zA-Z0-9).php $1 [QSA,L]

Assuming you have installed the rewrite module, you could put this in your virtualhost:
RewriteEngine ON
RewriteRule /newsdev/news /newsdev/news.php [L]
Check this site for more information on RewriteRule:
http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
But I doubt this is the best way. The rewritemodule is usually not the most efficient way to do easy things like these. Depends on what other complex things you want to rewrite.

With mod_rewrite
http://www.easymodrewrite.com/example-extensions
You need to work with the .htaccess file in your site root... you'll have to create one if it's not there.

Related

Rewrite rule for url in php in .htaccess file

I want to use rewrite rule for may website.
I have a link
www.mysite.com/product.php?p_id=1212
Need to convert like
www.mysite.com/product/1212
Please suggest me the rewrite rule for .htaccess file.
Thanks in advance.
please add this rule in your .htaccess file
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^product/([0-9]+)/?$ product.php?p_id=$1 [NC,L]

How i know .htaccess file is working or not?

I use this htaccess url
mywebsite.com/xyz/search.html
here xyz is a folder in root
in .htaccess is use the code for this url
# enable apache modRewrite module #
RewriteEngine On
RewriteBase /
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA]
now i want this
xyz/search.html is hit the
url xyz/index.php?page=search
but this:
RewriteRule ^([^//]+)/?(^/*)?.ht(m?ml?)$ index.php?page=$1 [L,QSA])
code is not working..
any idea regarding this...
Type some random characters in your .htaccess file and try to reload your page, if you see error 500 then your .htaccess file is working, put your random characters after "RewriteEngine On" .
Not sure that rule would work. Does this one do the job?
RewriteRule ^.*/(.*)\.html?$ index.php?page=$1 [L,QSA]
Also, if you want to test the rule with a bit more visibility, you can add R=302 to the flags, that way your browser will get a redirect and you'll be able to see the rewritten URL in the address bar
I wonder why people don't use RewriteLog.
Put in the same place:
RewriteLog /tmp/rewrite.log
RewriteLogLevel 3
It slows down the server but for debugging it's made for.
Try:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*?)/?([^/]+)\.ht(m|ml)?$ $1/index.php?page=$2 [L,QSA]
There is a very handy online tool for testing htaccess files. Simply paste your redicrect rules in the form provided and test various urls to see if they are redirected or left untouched.... soooper easy!

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 .

Rewrite URL using htaccess php

I want to rewrite the url in my project.
For example:
http://www.example.com/dashboard/test/ to http://dashboard.example.com/index.php
Also I want to do it for:
http://www.example.com/dashboard/test2/ to http://dashboard.example.com/index.php
Can anyone tell me the idea to rewrite the url?
First you create a .htaccess file in your root.
Than, just put the redirect commands in it.
How to compose a .htaccess and how to create rewrite rules is explained here: http://net.tutsplus.com/tutorials/other/the-ultimate-guide-to-htaccess-files/
You will need something like:
RewriteRule ^(.*)$ http://dashboard.example.com/$1 [L,QSA]
try this
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^test.*$ http://dashboard.example.com/index.php [R=301,L]

How to redirect in url by using .htaccess in php?

How to redirect in url by using htaccess in php?
http://www.example.com/randomstring/2
i would like to go to id page 20 thank
You should use mod_rewrite for example in this way:
RewriteEngine On
RewriteRule ^(randomstring)/([0-9]+)$ /randomstring/$1/ [QSA,R]
RewriteRule ^(randomstring)/([0-9]+)/$ index.php?page=$1&id=$2 [QSA,NC,L]
If you want to simply redirect from a domain to another just write:
RewriteRule ^(randomstring)/([0-9]+)$ http://www.example.com/$1/$2/ [QSA,NC,L,R=301]
use mod_rewrite, it's a module for apache. You can see the documentation here. .htaccess files control the webserver, not php, so you have to look there.
Copy that into your .htaccess file in the directory you need it:
RewriteEngine On
RewriteRule ([A-Za-z0-9]+)/([0-9]+)$ index.php?id=$2
www.domain.tld/asdf/2 --> index.php?id=2
www.domain.tld/asdfa923als/52 --> index.php?id=52
;-)

Categories