mod_rewrite from URL to GET variable - php

If someone tries to access this page:
www.myhomepage.com/m/40921
I want the URL (above) to remain the same but the content should be from this page:
www.myhomepage.com/msg?t=40921

If it must be exactly like you said, then use this
RewriteRule ^m/([0-9]+)\/?$ msg?t=$1
But I think you made a typing error. Shouldn't it be msg.php? If so, use this:
RewriteRule ^m/([0-9]+)\/?$ msg.php?t=$1
And in your php file, you can get the t with $_GET['t'].

Related

Error 404 when trying to create a pretty url using .htaccess in php

I want to create a pretty rule in php using .htaccess.
something like localhost/search/name instead of using the normal PHP way like search?q=name. But am stuck in implementing it because all my page are now returning error 404 and I don't know why.
Here is my .htaccess file:
RewriteEngine on
RewriteRule ^localhost/?$ search.php?name=$1 [QSA]`
Here is my search.php file:
<?php
echo $_GET['name'];
?>
So when I visit something like localhost/search/myname it returns error 404
What am I doing wrong?
You're going to want to first also include the /search/ as part of the rewrite rule and then i would also suggest adding in the 'L' flag after QSA so the rule is the last rule processed when called.
You then want to capture the name from the url and pass this through as a get param to the view.
I've also ommitted localhost from the URL as you don't need to target this.
try this:
RewriteRule ^search/(.*)$ search.php?name=$1 [QSA,L]

.htaccess re-write use a get variable to replace the filename

This one is a bit tricky for me, I want to make this URL as minimal as possible. Ideally I would like to change this URL:
http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School
to:
http://www.example.co.uk/something-school/
Using .htaccess file.
This means we would be using the school get variable to replace the .asp file name, getting rid of /profile/ as well as the other get variables.
Is this possible? If so how? If not could you potentially give me an alternative?
Any help would be greatly appreciated!
EDIT
A user Badhorsie has wrote a rewrite rule for me which does the exact conversion. Unfortunately the webpage fails to load as these get variables are unfortunately necessary for the page to load.
I am guessing it is not possible to hide the get variables. In which case would it be possible to retain the get variables but to keep them clean? Looks like the directory is also necessary?
Perhaps something like: www.example.co.uk/profile/something-school/1/1/2/3
We can get rid of the school get variable as it is not needed (only needed to replace the profile/profile.asp section.
Try this:
RewriteCond %{QUERY_STRING} (.+(?=&school))school=([\w%-]+)
RewriteRule ^profile/profile.asp /%2?%1 [L,NE,R=301]
Did you tried this RewriteRule. Try adding this code in your .htaccess file.
RewriteRule ^http://www.example.co.uk/profile/profile.asp?profile_id=1&top=1&abt=2&ft=3&school=Something%20School?$ http://www.example.co.uk/something-school/ [L]

Short URL with htaccess on many subfolders with same file

I have this situation:
There are many scripts that can be accessed like this:
http://website.com/script1/
http://website.com/script2/
Each script has a display.php file that receives a variable from the index.php file VIA a link. I'm using the $_GET method to reat that variable.
Example:
http://website.com/script1/display.php?id=124
What I'm trying to do is to access the display page like this:
http://website.com/script1/124
http://website.com/script2/124
Does anyone have any suggestions? My experience with htaccess is ~0.
RewriteRule script[\d]+\/[\d]+ /script$1/display.php?id=$2
This would rewrite anything like script345987/22352 to script345987/display.php?id=22352
Edit if you want to match anything, not only script, then:
RewriteRule (.*)\/[\d]+ /$1/display.php?id=$2

Generate URL Alias?? in PHP

I just saw this somewhere, and I'm interested on it, and can't seemed to find it anywhere or I just used the wrong words to search for.
Well I saw this link,
http://splur.gy/r/QqVYf/r/2tgNklHgmVK
and when I clicked it, I got redirected to other page which called
https://www.facebook.com/xxx.xxx?sk=app_xxxx
Anyone knows how this thing was made? or just a little hint to start off?
A help would be nice. :)
These are done with RewriteRule, a simple Google search willgive you mroe details.
In short, the URL will be broken down sorta like this: (Line 1, URL part, Line 2, PHP relative.
http://splur.gy
http://splur.gy/index.php
r
$_GET['var_1']
QqVYf
$_GET['var_2']
r
$_GET['var_3']
2tgNklHgmVK
$_GET['var_4']
The RewriteMod will take the URL as setup in the above format, and pass the varialbes to a script. It is another way of posting variables in the URL.
As you see above: stackoverflow.com/posts/15182831, does not actually have a file named posts/15182831, it is simple used as a variable, passed to a script which queries that database, and spits out results based on what the script says.
You will need to have a server that will allow you to rewrite requests so you can redirect all requests to a single script. If you are running Apache, you would create an .htaccess file with something like this in it:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^r$ /redirect.php [L,QSA]
RewriteRule ^r/(.*) /redirect.php?__q=/$1 [L,QSA]
</IfModule>
Then if you go to http://yourdomain.com/r/234243/adsfsd, the request will be sent to the script /redirect.php and '234243/adsfsd' will be passed as the GET paramiter 'q'.
Then you would create a file called redirect.php that would process the request and then redirect the user. It might look somthing like this:
<?php
$redirection = process_to_determine_location_from_query( $_GET['q'] );
header( 'Location: {$redirection}' );
?>
It's called a redirect. You can do it in PHP with this code:
<?php
header('http://example.com');
Another thing that might have happened is that the link you saw was not the actual link you follow when you click. It's as simple as doing this:
example.com
Anyone could do that.
http://www.google.com/
It has nothing to do with PHP.

rewriting issue with $_GET

i have something like
RewriteRule ^home$ ?get=main
inside my .htaccess to redirect any url calls from mysite.com/index?get=main to mysite.com/home
but when i use any quotation marks like ' and " after that url it dosen't behave correctly and dosen't access the file in question, for example if i type
mysite.com/home' or mysite.com/home" (notice the quotations) it redirects off to another page and dosen't access the file at all which should be mysite.com/index?get=main and when i use mysite.com/index?get=main' or mysite.com/index?get=main" it accesses those files then.
i want it to either access the mysite.com/index?get=main file from mysite.com/index?get=main" or give a 404 error page, right now it's doing neither, what's going wrong here?
The rewrite rule is looking for an explicit value of home to match to the ?get=main.
Try
RewriteRule ^home ?get=main

Categories