htaccess to rewrite url - php

So I heard you can use htaccess to rewrite the website url. For example I have a php page to view threads/posts which looks like this:
http://website.com/view.php?id=133
I have been told I could make it look like
http://website.com/view/133
I also have this link
http://website.com/delete.php?id=144
and I want it to look like this
http://website.com/delete/144
How would I go about doing this?
Thanks.

RewriteRule ([a-z])/([0-9])\ $1.php?id=$2
([a-z]) will replace your page name ([0-9]) will replace your id respectively in right hand side
Please refer
http://corz.org/serv/tricks/htaccess2.php

Yes, you can do all that using .htaccess.
Check out my answer Redirecting localhost/xxx to localhost/xxx.php in WAMP
I hope it helps.

RewriteRule ^(.*)/([0-9]+)$ $1.php?id=$2 [NC,L]
You have to give the url as http://website.com/delete/144, then it will be redirected to delete.php with 144 as id.
For example,
Delete

Related

Problem with .htaccess - RewriteRule is appending new URL to old URL

I'm very new to htaccess and mod_rewrite things, so please forgive me the possibly stupid question :-)
I have a Links like that on my menu
teams/senioren/herren-1
teams/senioren/damen-1
etc.
and I want to redirect it to
team.php?team=herren-1
team.php?team=damen-1
My htaccess looks like that:
RewriteEngine On
RewriteRule ^teams/([^/]*)/([^/]*)$ team.php?team=$2 [L,QSA]
That works on localhost when I click the link one time. The URL then gets https://localhost/teams/senioren/herren-1. But when I click a second time I get https://localhost/teams/senioren/teams/senioren/herren-1. So it looks like it appends the new URL to the old one.
Can anyone help me?
Thank you
Solution: Use absoluth Path like arkascha mentioned in the comments.
So in my case use /teams/senioren/herren-1 instead of teams/senioren/herren-1

RewriteRule - how to redirect from wrong to right address

I need to redirect such link:
www.website.com/index.phpsite=transport (it's a wrong link which was added to many catalogues of pages etc. and it's really important to redirect from this link to the right one)
to this one (obvious):
www.website.com/index.php?site=transport
All is about the missing "?" in address.
So I wanted to use .htaccess and RewriteRule, but dunno how.
Can you help me? How to write the rule that will replace wrong link with the working one?
Thanks in advance,
Konrad.
The following should work for you (although unfortunately I do not have time to fully test the code):
RewriteRule ^index\.phpsite=(.*)$ /index.php?site=$1 [R=301,L,NC]
I have used the .* wildcard on the end as I wasn't sure if you could have more URL parameters in some URLs you needed to redirect.
All this behaviour is documented in the related mod_rewrite manual pages.
If you are using Apache server, then Write a redirection rule in htaccess file.
RewriteRule URL1 URL2 [R=301,L,NC]
Since it worked, this is my answer:
RewriteRule ^index.phpsite=transport$ /index.php?site=transport [L]

changing the url with modifying the .htaccess file

I need help with modifying the url of a website I am working on.
The url is:
domainname.com/view-post.php?title=post-title&pid=2
I can only get it to say
domainname.com/2.html
but need it to say
domainname.com/posts/post-title
Any help will be greatly appreciated.
I would suggest you to use something like:
RewriteRule ^posts/(a-zA-Z0-9\-\.]+)/([0-9]+)/?$ view-post.php?title=$1&pid=$2 [NC,L,QSA]
or
RewriteRule ^posts/([a-zA-Z0-9\-\.]+)/?$ view-post.php?title=$1 [NC,L,QSA]
I found a tool called "URL Re writer Tool" which you can use to generate htaccess code: try it out here : http://www.webconfs.com/url-rewriting-tool.php It's perfect for those who want to Re Write the URLs on their server but don't know what to put in htaccess.

how to redirect a link to another link using htaccess?

i have a link like:
www.website.com/index.php?id=123456
and i want to redirect to:
www.website.com/test.php
the problem is that the id can be different each time, could be id=123456 or id=632456
any ideas on how to solve this?
thanks
edit:
what if i want to redirect it to www.website.com/test.php?id=123456 or whatever that id is?
Something like this:
RewriteRule index\.php.* test.php
Or you can be more specific:
RewriteRule index\.php\?id=[0-9]+$ test.php
Or you can even send the id too:
RewriteRule index\.php\?id=([0-9]+)$ test.php?id=$1

how can I rewrite URL to fancy url?

when my user logs in, I want the get variables that were sent rewrote onto the URL like so:
http://mysite.com/mygetvar1/mygetvar_value1/mygetvar2/mygetvar_value2/
or
mysite.com/mygetvar1=mygetvar_value1/mygetvar2=mygetvar_value2/
How can I do this?
Please help! Thanks!
Codeigniter can offer you like that. Many other PHP frameworks offer that as well.
Try this.
RewriteRule /([^/]*)/([^/]*)/([^/]*)/([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
RewriteRule /([^/]*)=([^/]*)/([^/]*)=([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
First you need the mod_rewrite module enable.
After, put this in your config file :
RewriteEngine on
RewriteRule ^ads/(rims|tires|combo)/([0-9]+)/(.+).html /ad.php?noAds=$2 [L,QSA]
This is a example.
Your url will look like : http://www.yourwebsite.com/ads/rims/331/title.html
but you will call the url : http//www.yourwebsite.com/ad.php?noAds=331
For your regex , you should use a site like http://www.rubular.com
You can use the .htaccess file or put in directly in the httpd.conf file

Categories