Hey everyone. Hopefully I can explain this correctly.
I have some URL's which I would like to tidy up through URL reWriting. For instance, I have:
domain.com/index.php?page=1
domain.com/index.php?page=2
domain.com/index.php?page=3
domain.com/index.php?page=4 etc..
which I would like to be shown in the URL as:
domain.com/page/1
domain.com/page/2
domain.com/page/3
domain.com/page/4 etc..
Also a quick question:
-Will the $_GET query's still be executed if the URL is rewritten?
Many thanks ahead of time. I have the modules enabled and the .htaccess is ready to go. i just need some direction. Thanks!
Add this to your .htaccess file:
RewriteEngine On
RewriteRule ^page/(\d+)/?$ /index.php?page=$1 [L]
The GET queries will always be executed. If the user types in domain.com/index.php?page=3, then index.php will run. If they type in domain.com/page/3, then mod_rewrite will convert it to domain.com/index.php?page=3 before passing it to PHP. Either way, index.php is run.
Related
I have got different pages in my website(portal) which may take one or more parameters from the URL.
So the URL www.example.com/xyz has to be solved into or like www.example.com/profile.php?id=xyz and the value id should be able to get using GET method.
I'm working on PHP. Any answers would be highly appreciated. (I'm new to PHP and have only basic+ knowledge in it. Please explain briefly.)
To go from: www.example.com/profile.php?id=xyz into www.example.com/xyz you can use the following rule in your .htaccess file:
RewriteEngine On
RewriteRule ^([^/]*)$ /profile.php?id=$1 [L]
Just make sure you clear your cache before testing this.
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]
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.
How do I strip a part of the url? I do not know much about htaccess or apache.
I would like to strip www.mysite.com/page=services to www.mysite.com/services for example.
What exactly do I need to put in the .htaccess file in order to achieve this, and would that work for other pages as well?
Thanks.
I tried this for one of my sites lately and came up with this and for me it works fine,
this goes into the .htaccess file:
RewriteEngine On
RewriteRule ^([A-Za-z]+)/?$ index.php?page=$1 [NC,L]
then if you would write yoursite.com/pagename it would send yoursite.com/index.php?page=pagename to your php.
You will link to the page like so: yoursite.com/pagename
it wont change yoursite.com/index.php?page=pagename to yoursite.com/pagename in the adress bar after you send it.(if that makes sense :) )
I hope this is what you are looking for...
I think you mean your original URL to be www.mysite.com/index.php?page=services and not www.mysite.com/page=services
Also, you probably mean the opposite, you should switch www.mysite.com/services to www.mysite.com/index.php?page=service
Anyway, to change www.mysite.com/services to www.mysite.com/index.php?page=services then you need .htaccess, and the rule for that would be RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L]
As suggested earlier, you should read about .htaccess, regex, and rewrite rules. Best resource is the apache documentation here: http://httpd.apache.org/docs/2.2/howto/htaccess.html
I read your comment earlier that you need the opposite, I am not sure why you need that, because the whole idea of URL shortining is to make easy-to-remember URLs in addition to some security concerns that can be resolved. The URL is the first thing that is sent to load your webpage, then .htaccess changes it to some form undrstandable by PHP then PHP deals with get parameters for example.
I noticed on youtube their url does not have a file extension and querystring. I've been trying to emulate something similar but found I had to either include the file extension or a trailing slash.
members.php?agefrom=20&ageto=40&city=london (works)
members/?agefrom=20&ageto=40&city=london (works)
members?agefrom=20&ageto=40&city=london (doesnt work)
So I was just wondering how can I get the third case to work? i've tried a few things in the htaccess file.
RewriteRule ^members$ index.php?page=members&%{QUERY_STRING} [QSA,L]
I have tried the above but to no avail.
Any help would be appreciated.
The RewriteRule that you posted is correct for members.php? and for members? It should not work with members/
You must have additional RewriteRules before this one that are getting applied first and are affecting this rule.
However, here is a rule that should still work for you:
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
The /? is saying to match if the slash exists or if it doesn't exist.
Have you tried to remove the $ on the end?
RewriteRule ^members/?$ index.php?page=members&%{QUERY_STRING} [QSA,L]
This did work in the end, all I had to do was move it nearer the top of the htaccess file. I had the following line which I guess was being read instead.
....
RewriteCond %{REQUEST_URI} ^/members$ [OR]
....
I am changing my approach to SEO URL's because I was trying to find articles on how the googlebot actually crawls forms and how it prefers the GET method. I was using jquery to alter my action parameter to write the following URL:
/members/london/18-to-25
I dont know how much google likes jquery and whether it would scan javascript code. I am assuimg it just follows the HTML code and having done some research I have changed my form to use the GET method and so the bot can crawl my form without complaining so now my URL looks like this:
/members?location=london&agefrom=18&ageto=40
I am on the right track to assume this? or should I just stick with jquery to rewrite the action parameter for an seo friendly URL?