mod_rewrite url - php

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?

Related

Deleting all php extension and get rid of variables in the url

I got this url
/localhost/andalucia/productdetails.php?value=20
I want to change it to
/localhost/andalucia/productdetails/20
how do you do this and how will you get the value 20 in the handler page?
should I change the coding or I just add an ht access file?
If it is adding a ht access file what code should be in it?
How if I have more pages like:
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
Will they be affected automatically too?
ok i tried to use
RewriteRule ^productdetails/([0-9]+)$ productdetails.php?value=$1 [L,QSA]
but now the problem is all my pictures is gone in the html and i cant open the other page like
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
i need a htaccess code that can open all of these
/localhost/andalucia/product
/localhost/andalucia/home
/localhost/andalucia/contactus
/localhost/andalucia/productdetails/20
pls helpp someone
With the apache extension mod_rewrite it is really easy to transform your pretty URLs into the URLs needed by your script. This .htaccess example which you place in your web root should get you going:
RewriteEngine On
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [L]
This example will only rewrite andalucia/productdetails/NNNN... format URLs, all other URLs won't be affected. If you need to pass other query parameters, like /andalucia/productdetails/20?sort=asc you need to pass the QSA flag (query string append) to the rewrite rule:
RewriteRule ^andalucia/productdetails/([0-9]+)$ /andalucia/productdetails.php?value=$1 [QSA,L]
The L flag will prohibit the evaluation of next rules. Just look up the mod_rewrite documentation for a in-depth discussion!

mod_rewrite for specific url only

I'm into rewriting a url in different ways. I mean I want to know how to use mod_rewrite so I can do the following:
1- convert a .php to html for a speific url
i.e: from www.mydomain.com/news.php to www.mydomain.com/news.html
I found some interesting codes, but not sure which one works without any errors...
some of what I found:
RewriteEngine on
RewriteRule ^(\d+)/(\w+).html$ index.php?id=$1&title=$2
2- convert any sub url of that news.php file to
news.php?do=news&id=24455
so the topics or threads show like this without slashes /
I find the 2nd question a lot difficult, but sure there must be a solution for that.
any idea how to get both questions done for a specific url like what I stated above...!!
Thanks
convert a .php to html for a speific url i.e: from www.mydomain.com/news.php to www.mydomain.com/news.html
RewriteRule ^news\.html$ /news.php
Guess for the second (assuming you want to rewrite /news/foo.html urls):
RewriteRule ^news/(.*)\.html$ /news.php?id=$1

How do I strip part of a url?

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.

.htaccess RewriteRule for Flat Links

I am pretty new to using the RewriteRule, so I am likely missing something obvious, but I have a PHP script that takes URL variables like this:
{baseurl}properties.php?prop=Property-Name
I would like to create RewriteRules so that anyone who types in this script name/variable combo would have their URL rewritten to:
{baseurl}/properties/Property-Name
As well as ensuring that anyone who types in the flat-link url, actually calls the script with the right variable name and value.
I have been referring to this link and I have found related threads:
Mod_rewrite flat links
Mod_rewrite trouble: Want to direct from ?= to a flat link, nothing seems to work
But, I am obviously doing something wrong, as I cannot get this URL to work the way I want. I am currently using the following code, which appears to do nothing (aside from rewriting the URL to include the www, and redirect requests for index.php to the site root):
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^baseurl.com$ [NC]
RewriteRule ^(.*)$ http://www.baseurl.com/$1 [R=301,L]
RewriteRule ^index.php / [R=301,L]
RewriteRule ^properties/([0-9A-Za-z]+)/$ /properties.php?prop=$1
The issue is clearly with the last RewriteRule, assuming nothing above is affecting it. Again, I am likely doing something ridiculous. Can someone please explain what I am doing wrong?
Thanks for your help.
At a quick glance, it appears that you forgot to include the dash in your regular expression and you included trailing slash. Use this instead:
RewriteRule ^properties/([0-9A-Za-z-]+)$ /properties.php?prop=$1
If you look at your rule ^properties/([0-9A-Za-z]+)/$ you see that it needs to end with a forward slash. You can either remove that or make it optional like ^properties/([0-9A-Za-z]+)/?$.

how do i hide var passing info in the url if i have no form tags?

Hey guys - im using var passing links like this to jump around a site...
<a href = "index.php?content=about.html">
...problem is, i have all the ugly var info visible in the url. I would usually hide it by using the post method, but i dont have any form tags, so is it even possible?
Thanks!!!!!!!
It's a bad idea. GET is used for reading, POST is used for updating. A better solution would be to use some sort of mod_rewrite to make friendly URLS. Often called SEO friendly URLS...
Yes, you can POST with a <a href... but you have to have a lot of ugly javascript to do it... which breaks all sort of standard conventions.
Update, combining some new information
#FDisk has the simplest solution below, but I would add a condition to it which would allow existing files to be passed through directly by the webserver without having to run it through PHP:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
That way, a request to /images/bar.png will be served directly from the filesystem if that image exists.
Note, that your page does not necessarily need to have ".html" on the end anymore. So your URL could look like: http://example.com/about which would then be converted to: index.php?content=about
Taking it one step further from the link you listed in your post, you could then parse the url for various parameter. The example you looked up stuffed them into [$_GET, $HTTP_GET_VARS, $_REQUEST] respectively, but I think that's not such a good idea. Just make your own array of parameters.
You can try using the mod_rewrite extention
The original URL:
http://www.youwebsite.com/index.php?content=about.html
The rewritten URL:
http://www.youwebsite.com/about.html
.haccess file content:
RewriteEngine On
RewriteRule ^([^/]*)$ /index.php?content=$1 [L]
you can hide info (var name and content) by encoding it. Thus the user won't be able to understand or change what you are passing around. But he will still see something in his url.
I guess you should give use some more context to understand why you cant use direct links to static pages ?

Categories