.htaccess redirection - php

Hello I want to have some redirection made by .htaccess file:
Some examples
mysite.com/accepted -> mysite.com/index.php?type=accepted
mysite.com/waiting -> mysite.com/index.php?type=waiting
mysite.com/cancelled -> mysite.com/index.php?type=cancelled
&
mysite.com/edit/2 - > mysite.com/admin.php?edit=2
ETC. for all numbers possible
mysite.com/login -> mysite.com/admin.php?action=login
mysite.com/register -> mysite.com/admin.php?action=register
mysite.com/lostpassword - > mysite.com/admin.php?action=lostpassword
mysite.com/add - > mysite.com/add.php
Apprectiate your help ;)
#edit: It should be done that way but with masking urls.

Which way around is the redirect? It would be common to see redirects from your given right-hand-sides to the left-hand-sides, but not the other way around.
EDIT I see you've fixed that, so my supposition was correct:
i.e. you'd normally see:
RewriteRule ^edit/([0-9]+)$ /admin.php?edit=$1 [L]
etc, to map the nice friendly RESTful style URL into the internal URL.

If you want to get into the topic yourself, www.modrewrite.com hosts a number of great mod_rewrite resources with lots of examples.

If I understand your edit correctly you want this
<?php
header('Location: http://mysite.com/'.$_GET['type']);
?>

Since there is no structural pattern in your URLs that can be used to map the URLs to the destinations, you probably will need to use one rule for each group like these:
RewriteRule ^add$ add.php [L]
RewriteRule ^(login|register|lostpassword)$ admin.php?action=$1 [L]
RewriteRule ^edit/(\d+)$ admin.php?edit=$1 [L]
RewriteRule ^(accept|waiting|canceled)$ index.php?type=$1 [L]

Maybe something like the following?
RewriteRule ^www.domain.com/([^/]+)/([^/]+)/([^/]+)/([^/]+) /index.php?$1=$2&$3=$4 [NC]
Do note that you will have to parse the URL (REQUEST_URI) instead of accessing $_GET

Related

PHP Mod_rewrite

i'm new to mod_rewrite, and i'm trying to convert my web address from:
website.com/profile.php?user=andy
to the following:
website.com/user/andy
This is my following code:
RewriteEngine On
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?user=$1 [NC,L]
I researched extensively and this does seem to be the correct way to do it, but it doesn't redirect to where i want it to, it just redirects to this:
http://website.com/profile.php?user=andy
which means i must doing something wrong...
Can anyone help me out here? I would appreciate any tips.
If you want
http://website.com/profile.php?user=andy ->301-> http://website.com/user/andy
http://website.com/user/andy means http://website.com/profile.php?user=andy
They are 2 different things, you'll need 2 rules:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^user=([A-Za-z0-9]+)
RewriteRule ^profile.php /user/%1? [R=301,L]
RewriteRule ^user/([A-Za-z0-9]+)/?$ profile.php?a=b&user=$1 [L]
The first will 301 (moved permanently) redirect to the pretty url.
The second will allow your application to understand the pretty url.
Whenever you change the url scheme for a site you should take care of existing links. As such, that first rule is required/a good idea. You should not, however, need the first rule when using your own application. If your own application is generating links to profile.php?user=me - change your application code.
You have to change your URLs when outputting them in your HTML to be in the format you want (/user/andy).
mod_rewrite will rewrite /user/andy to main.php?... not the other way around.
What do you mean by my result?
mod_rewrite won't change existing links in your source code. Navigate to website.com/user/andy and you should see it work.

.htaccess with pagination in php?

I am currently coding a pagination script into many parts of my site, this has been a well needed and requested feature and I have finally been able to come round and start coding it, it is all going well, until I find that my rewritten urls don't like working with the pagination urls.
So, an example page on my site would be news.php. This file structure can be something like news.php?id=5. I have rewritten the url like so:
/news/5/
## Rewrite URL's for News & Dev ##
RewriteRule ^news/([0-9]+)/$ /news.php?id=$1 [L]
RewriteRule ^news/([0-9]+)$ /news.php?id=$1 [L]
RewriteRule ^news$ /news.php [L]
RewriteRule ^news/$ /news.php [L]
The pagination script I am using prepends two new variables in the url, the new url turns out to be this:
news.php?page=1&ipp=55id=5
I would really appreciate it if anyone could assist me in making the urls look better, as it defeats the object of having it in the first place if after they use the pagination, it changes the url back to a clunky and ugly url.
I don't want it to be required to have parts of the url, that is something I really don't want..
e.g I don't want the url to be required to be /news/1/55/5, instead id like it to be optional.
Thank you for your time, it is appreciated!
Additional Information
The links in my news script currently display like so:
news.php?page=1&ipp=55id=5
I don't like to see ugly urls like that, and want to make the url look better using mod_rewrite, It would be better if the urls would display like so:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
Also, to make it as user friendly as possible, I don't want any of the fields to be required as such, so for example I would like to have the following link accessible at all times without it requiring the other fields.
/news/ID/
Then, when the user clicks a pagination link, it would use the following link structure:
/news/PAGE/IPP/ID/ -> return news.php?page=1&ipp=55id=5
This is all from user feedback of my site, and is something that people have been asking for. Problem is, I don't understand even simple .htaccess
Thanks
RewriteBase /
# add slash to end of url if not present (and do a redirect)
RewriteCond $0 !/$
RewriteRule ^news([^\.]*)$ $0/ [L,R=302]
# rewrite url with format /news/[<id>/[<page>/[<ipp>/]]]
RewriteRule ^news/(?:([0-9]+)/)?(?:([0-9]+)/)?(?:([0-9]+)/)?$ /news.php?id=$1&page=$2&ipp=$3 [L]
Not sure what ipp is supposed to be, but my guess is it shows the number of item per page. I would personally not like to have that in my url.
You can have :
news/id/page/ipp with
RewriteRule ^news(/?)$ news.php [L]
RewriteRule ^news/([0-9]+)-(.*)_([0-9]+)(/?)$ news.php?page=$1&ipp=$2&id=$3 [L]
news/1222-subjet-for-example_34
return :
news.php?page=1222&ipp=subject-for-example&id=34
use (/?) instead of create many rules ;)
Hope it's works for you.

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]

Custom URL with PHP

I have a small question to ask. Is it possible, via php or htaccess, to change a url like: miodominio.com/users.php?idu=x into something like miodominio.com/username ?
I want it to be Facebook style...Where "username" is the username chosen by idu = x.
There are multiple ways to solve this problem, but here's one that always suits my needs.
Guide all your URL requests through the index.php first and resolve the request in your PHP code second.
1) Use an .htaccess file to direct all URL's through index.php. You'll find one way here by the CodeIgniter framework and a more advanced explanation here. I recommend the CodeIgniter .htaccess guide first if you're inexperienced with .htaccess.
2) Second, use the $_SERVER variable in PHP to extract the URL. Probably with the help of the $_SERVER['REQUEST_URI'], you'll find '/username/' which you can then use to extract the user's data and serve it to them.
Good luck and beware of URL injections using this method.
You need to use apache's mod_rewrite for this. It can translate miodominio.com/username to miodominio.com/users.php?idu=x. There are some good guides about this which are easy to find with Google.
You can try to use this mod_rewrite pattern (add it to the .htaccess):
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ users.php?idu=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ users.php?idu=$1
you have to write a clean URL in your .htaccess file like :
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ users.php?idu=$1
Put the following in your .htaccess
RewriteEngine on
RewriteRule ^([a-z0-9_-]+)/?$ /users.php?idu=$1 [NC]
The [NC] will make it case-insensitive, if you accept only lowercase username, remove the [NC] from the last.

remove http from URL in .htaccess (mod_rewrite)

Been stuck on this for ages and tried loads of fixes but just can't get my head around it!
I run a site where the content of the pages are generated based upon a URL. For example:
http://www.mysite.com/http://www.supercheese.com
Would generate a mashup of content from mysite.com and supercheese.com
The .htaccess i use for this (at mysite.com) is:
<ifModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule .* - [L]
RewriteRule assets/.* - [L]
RewriteRule ^(.*)$ test.php?url=$1 [NC,L]
</ifModule>
So basically the second URL is passed in a php string.
My question is this, I need to remove the http:// from the address using .htaccess
E.G. If someone types:
http://www.mysite.com/http://www.supercheese.com
I need it to become:
http://www.mysite.com/www.supercheese.com
Many thanks in advance for taking a look at this. You guys rock.
You can simply use a RedirectMatch
http://httpd.apache.org/docs/2.0/mod/mod_alias.html#redirectmatch
Try with :
RedirectMatch ^/https?://(.*)$ http://www.mysite.com/$1
Edit : you have to put this before rewrite rules
Edit : add / before http
Edit : David is right, take a look a his answer and change your way of writing these urls
It looks to me like the url scheme is inherently problematic.
Using an unencoded colon : in the url - other than following the http to specify the access protocol - seems to make the server think that it is doing authentication of the form http://username:password#hostname/.
I know it doesn't directly answer the question, the solution is to change the url-scheme. ;-(

Categories