Create pretty url's with GET variables - php

At this moment I have a situation in which pretty url's are rewritten so that everything goes by the index.php, while maintaining the $_GET variables. The htaccess looks as follows:
RewriteEngine On
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3&type=$4
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2&sub=$3
RewriteRule ^([^/\.]+)/([^/\.]+)?$ index.php?p=$1&projectid=$2
RewriteRule ^([^/\.]+)/?$ index.php?p=$1
This works fine. However, I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123.
Is this possible, and if so, how? All I've managed so far is creating a redirect loop.
For those interested, the redirect loop I created looks as follows:
RewriteCond %{QUERY_STRING} ^p=([^&]+)
RewriteRule ^/?index\.php$ http://www.example.com/%1? [L,R=301]

I want it to work the other way around as well. If someone goes to http://www.example.com/index.php?p=page&projectid=123 that they will be redirected to http://www.example.com/page/123
Yes it indeed possible but you will need to use THE_REQUEST variable for that.
RewriteCond %{THE_REQUEST} \s/+index\.php\?p=([^\s&]+)&projectid=([^\s&]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]
You can build similar rule for other URLs as well.

Related

Redirecting multiple URLS with parameters via htaccess

I've seen plenty of answers here about this question but none seem to help me.
I have a list of about 300 URLs that all have parameters in them. I want to redirect each one to different URLs on a new domain. The thing is, the redirection is determined by the content of the page, not param name = page name or something of the sort.
So, I have a list of old page = new page, how would I create something like that in htaccess?
If there were no parameters I would do:
Redirect 301 /old-page-name/ http://newurl.com/new-page-name
however that doesn't work.
Any ideas?
Edit:
I tried this and it doesn't work. Possibly this would be easier for anyone to help out with?
RewriteCond %{HTTP_HOST} muflaim\.co\.il [NC]
RewriteCond %{REQUEST_URI} ^/shop/classic?page=shop.product_details&category_id=&flypage=flypage_experience.tpl&product_id=200$
RewriteRule ^(.*)$ http://www.dreambox.co.il/45217/ [R=301,L,NC]
Just in case anyone finds this helpful, this did the trick:
RewriteEngine on
RewriteCond %{HTTP_HOST} muflaim\.co\.il [NC]
RewriteCond %{QUERY_STRING} ^page=shop.product_details&category_id=&flypage=flypage_experience.tpl&product_id=200$ [NC]
Rewriterule ^shop/classic$ http://www.dreambox.co.il/45217? [R=301,L,NC]

Mod_rewrite url vice versa redirect

I can't understand and can't find the needed clear information about it.
Is it even possible to rewrite with mod_rewrite localhost/mysite/index.php?a=search url to localhost/mysite/search?
I have tried with wikipedia http://en.wikipedia.org/w/index.php?title=Dog, then it immediately redirects to https://en.wikipedia.org/wiki/Dog. I want similar thing.
I've tried this code on my localhost site .htaccess file:
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L]
it worked when I tried to type in browser something like localhost/mysite/search but it is only one part of what I want. Please answer, I'm absolutely exhausted.
You need an additional redirect rule for redirecting to pretty URL. Have it this way:
RewriteEngine On
RewriteCond %{THE_REQUEST} /index\.php\?a=([^\s&]+) [NC]
RewriteRule ^ /%1? [R=302,L,NE]
RewriteRule ^([a-z]+)/?$ index.php?a=$1 [NC,L,QSA]

htaccess rewrite causing infinite redirect

I need to rewrite a URL from something like
/index.php?option=com_scoreboard&view=scoreboard&agent=001C0000016rJeUIAU
to
/quote/?agent=001C0000016rJeUIAU.
Here's what I've got so far.
RewriteCond %{QUERY_STRING} agent=(\w+)&?
RewriteRule ^index.php /quote/?agent=%1 [R=301,L]
Which works fine and dandy, but it ends in an infinite loop. And I know why too, it's because it keeps finding agent=. What should I be adding to my rewrite rules to stop this?
I've also tried variations like
RewriteCond %{QUERY_STRING} ^option=\w+?&agent=(\w+)&?
RewriteRule ^index.php /quote/?agent=%1 [R=301,L]
But it ends in the same infinite redirect.
This is for a Joomla site as well, if that helps. So after this rule is the standard Joomla rewrites.
Thanks a bunch!
Bette to THE_REQUEST variable instead and make sure to keep this rule as the first rule:
RewriteCond %{THE_REQUEST} /index\.php\?agent=(\w+)
RewriteRule ^ /quote/?agent=%1 [R=301,L]
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.

url rewrite and redirect - php

I recently developed a web site using php which accepts a id as its input via get.
http://website.com/profile.php?id=123. its the old fashion way, I've seen sites use pretty url's like http://website.com/username.html
So in order to archive this, i managed to do some research and end up with this code blow.
Also i'm passing the username in the query string.
http://website.com/profile.php?id=123&username=test_user
the parameter ID is only used as a input for the profile.php
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /profile.php [NC]
RewriteCond %{QUERY_STRING} ^id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^.* http://%{HTTP_HOST}/%2.html [R=301,L]
The final output i seek is http://website.com/username.html
Is their anything wrong in this ? this doesn't seem to work. Am i doing something wrong ? if so please be kind to let me know where my error lies. Thank you.
Try this rule:
RewriteEngine On
RewriteRule ^profile/([^/\.]+)/([^/\.]+).html/?$ profile.php?id=$1&username=$2 [L]
then you should be able to use
http://website.com/profile/123/username.html
as the URL. You can ignore the profile part but I don't recomment it because it would clash with other URLs of your website.
If you only need the username, use
RewriteRule ^profile/([^/\.]+).html/?$ profile.php?username=$2 [L]
and the URL would be
http://website.com/profile/username.html
You can use this rule:
RewriteEngine On
RewriteCond %{THE_REQUEST} /profile.php\?id=([^&]+)&username=([^&]+) [NC]
RewriteRule ^ /%1/%2.html? [R=301,L]
RewriteRule ^([0-9]+)/([^/.]+)\.html$ profile.php?id=$1&username=$2 [L,QSA,NC]

mod_rewrite: rewrite existing files to friendly links

I went through a bunch of websites and tutorials yet can't find a solution.
Following snippet works and http://example.com/page/pot return a pot.php content
RewriteEngine on
RewriteRule ^page/([^/]*)$ $1.php?page=$1 [L]
I can't get it to work the other way around
RewriteEngine on
RewriteRule ^([^/]*)/page$ $1.php?page=$1 [L]
Your current approach will cause infinite looping since Apache re-injects rewritten URI back for further rule processing.
You need to use THE_REQUEST variable for that like this:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page/[^.]+\.php\?page=([^\s&]+) [NC]
RewriteRule ^ page/%1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/]*)/?$ $1.php?page=$1 [L,QSA,NC]
Try to use:
RewriteEngine on
RewriteRule ^([^/]+)/page$ $1.php?page=$1 [L]

Categories