Any issues with using wildcards in htaccess file? - php

I have the following rewrite in my htaccess file:
RewriteRule residential-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)-(.*)\.html$ http://MYDOMAIN.COM/listing.php?type=rent&recordID=$7
I then use a function to build the final URL - here is an example of one possible URL:
residential-house-for-rent-sa-adelaide+hills-aldgate-895.html
The reason I have used wildcards is because there are many thousands of possible combinations which I do not want to explicitly state in the htaccess file. The only element within the URL that actually controls the final output of the page is the recordID.
My question is - are there any issues that I should be aware of with using so many wildcards in building the URL ? My concern is that ultimately a user can type anything as a URL, so long as it fits with the patterns required in the htaccess file and so long as a record ID is in position $7, and it will reach a page. I am unsure if this will have any detrimental impact on SEO or Google crawling of the site or whether there are any other potential issues that I need to think about with this structure ?
Any help is appreciated :-)

So I have looked into this and so long as you are careful not to create rewrite rules that contradict each other then I can see no issue. On a side note, I have read that it is not a great idea to have too many rewrite rules in the .htaccess file as it slows things down a little.

Related

PHP Get method without?

Hey so im working on a website and one part of it allows you to lookup a user based on their name. At the moment i have it using a $_GET request so the link would look like:
http://website.com/p?name=John+Smith
How would i be able to remove that ?name= because i see alot of sites doing things like:
http://website.com/p/John+Smith
how would i achieve this because to my knowladge their arent any other forum request types only Post and Get?
URL rewriting is definitely what you're looking to do. It's well worth playing carefully with it but lots of testing is recommended. With great power comes great responsibility!
Most dynamic sites include variables in their URLs that tell the site what information to show the user. The example you provided is exactly like this.
Unfortunately, a cleaned up URL cannot be easily understood by a server without some work. When a request is made for the clean URL, the server needs to work out how to process it so that it knows what to send back to the user. URL rewriting is the technique used to "translate" a URL like the last one into something the server can understand.
To accomplish this, you need to first create a text document called ".htaccess" to contain the rules. This would be placed in the root directory of the server. To tell the server to rewrite a URL pattern, you need to add the following to the file:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^p/[A-Za-z\+]$ /p/?name=$1 [NC,L] # Rewriting rule here
The NC bit denotes case insensitive URLs and the L indicates this is the last rule that should be applied before attempting to access the final URL.
You can do quite a bit with this one rule, but the specifics extend far beyond the space of my answer here.
https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/
I would highly suggest reading that thorough guide to help you on your quest!

Url rewrite without changing the paths to assets

I am new to url rewrite . I have a completed site with urls like abc.com/abc.php?id=54&title=abcd. First of all i need friendly urls for SEO like abc.com/abc/54/abcd. I know that can b accomplish by redirecting urls in .htaccess (But i have to change all my urls in code too). can i accomplish that without changing all my code url. Second how to not disturb the assets.
Need Help .
Thanks
You must change all your URLs in the code.. The other functionalities should not be affected as the .htaccess rewriting will point to the old URLs.
You could find an workaround to redirect the user with javascript but this is not recommended for SEO. Something like:
abcd
JS (jQuery):
$(document).ready(function(){
$('a').click(function(e){
e.preventDefault();
// parse the url to get its components
window.location = '/' + parsedURL.filename + '/' + parsedURL.queries['id'] + '/' + parsedURL.queries['title'];
});
});
You can use this rule in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteRule ^([\w-]+)/([\w-]+)/([\w-]+)/?$ $1.php?id=$2&title=$3 [L,QSA]
This will let you use abc.com/abc/54/abcd in browser and it will internally route to abc.com/abc.php?id=54&title=abcd.
I know this will be a very controversial answer, but bear in mind... this is Googles answer to this question.
For so many people, they believe (because they've been taught) that dynamic URLs like page.php?id=3&entry=65 are bad for SEO.
However, that was before... and this is now.
In fact, with the progress that has been made by search engine crawlers such as "googlebot", the opposite is true.
It is best to leave your dynamic URLs alone and not rewrite them (for SEO purposes).
This excerpt is from Google Webmaster Central...
Myth: "Dynamic URLs cannot be crawled."
Fact: We can crawl dynamic URLs and interpret the different parameters. We might have problems crawling and ranking your dynamic URLs if you try to make your urls look static and in the process hide parameters which offer the Googlebot valuable information. One recommendation is to avoid reformatting a dynamic URL to make it look static. It's always advisable to use static content with static URLs as much as possible, but in cases where you decide to use dynamic content, you should give us the possibility to analyze your URL structure and not remove information by hiding parameters and making them look static.
Does that mean I should avoid rewriting dynamic URLs at all?
That's our recommendation, unless your rewrites are limited to removing unnecessary parameters, or you are very diligent in removing all parameters that could cause problems. If you transform your dynamic URL to make it look static you should be aware that we might not be able to interpret the information correctly in all cases. If you want to serve a static equivalent of your site, you might want to consider transforming the underlying content by serving a replacement which is truly static. ... However, if you're using URL rewriting (rather than making a copy of the content) to produce static-looking URLs from a dynamic site, you could be doing harm rather than good.
You can read the full article here
I found this simply by searching the term "rewrite URL for dynamic pages". (on yahoo... not Google)
And this article by Barry Schwartz.
Barry Schwartz is Search Engine Land's News Editor and owns RustyBrick, a NY based web consulting firm. He also runs Search Engine Roundtable, a popular search blog on very advanced SEM topics.

A Dynamic website using PHP

I am a beginner PHP programmer. I searched google for a "Dynamic PHP website tutorials". I found some stuff. They use $_GET variable to make the website dynamic, so the URL's appear like this:
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
and so on...
But most of the dynamic websites that I found on the internet has links like this:
example.com
example.com/about
example.com/download and so on....
So how do they do so ?? Have they got folders for all the catogories ?? And Also some websites have article URLs (eg : example.com/articles/posts/2010/article1.php). It would be a reall mess if they've got folders for all items. If not then How ?? Can someone give an example please ?
If you're using apache then read: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
If you're using IIS then read: http://www.iis.net/downloads/microsoft/url-rewrite
In order to use the $_GET variable, it must be in the query string (or being routed through some other means that isn't 'default').
For example, the URLs you're using would become.
example.com/?page=home
example.com/?page=about
example.com/?page=Downloads
Additionally, you can rewrite URLs using the .htaccess file (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html)
You are interested in page routing.
htaccess and MVC routing may start you down the correct path :)
To echo everyone else, it's called a url rewite.
For example, the url
http://example.com/index.php?ext=blog&cat=news&date=12122012
can be rewritten as
http://example.com/blog/news/12-12-2012
This isn't automatic, it requires defining the patterns used for understanding the new URL in a file called .htaccess which usually resides in the servers root directory. Note that the preceding '.' in the filename makes it a hidden file.
When I was first getting used to PHP i found the site http://phpbuilder.com a great help. They have a lot of articles, and a forum that is fairly nice to noobies. http://devshed.com is a good site too, and has a large amount of information on subjects outside of PHP.
You can achieve that affect with folders, but most use rewrites (Apache). It's a bit too broad of a subject to go in to here, but if you just search for rewrite tutorials you'll find some pretty quickly.
The $_GET is only to get variables from the URL. While this can be used to make sites dynamic, this is a technique which is usually frowned upon.
With rewrites, you basically have a URL like /about, but the rewrite tells your server something like "act like this is actually ?page="about"), which you then use the $_GET to process.
Being PHP beginner I will not urge you to use .htaccess, As you will need to learn lot many things before you proceed further. You have 2 option to send a request one is GET and POST. You can get more information about same on internet.
Also you have an option to start your dynamic website using CMS and I will recommend you to use wordpress. CMS will have some in-built function which will help you to do your work faster. Also using their control panel you can update the URL format.
I will also urge you to go step by step and follow every tutorial that you will find on internet.
All the best
If you want to do this you have to use .htaccess file and have to load mod_rewrite in your apache server.
In you root directory create file named .htaccess
Then Write:
RewriteEngine On
RewriteRule ^(.*)\.php$ index.php?page=$1 [L,QSA]
And After that call a page
my-page.php
It will redirected as a index.php?page=my-page internally but in browser it will show as my-page.php

How To Make PHP Application To Use Actual URL's If Mod-Rewrite Is Disabled?

I am working on a PHP Application, Every thing works perfectly, The only problem is.
I have enabled SEO Friendly URL's, Which re-writes the actual URL's to virtual URL's( i know you guys know it )
Ex : hxxp://www.website.com/index.php?page=about-us
To
hxxp://www.website.com/page/about-us/
What i want to achieve is If the SEO URL's / Mod Rewrite is disabled, the user should be able to access the direct/actual URL's.
In brief, If Mod-Rewrite is enabled, the web application should automatically use the SEO Friendly URL's otherwise go with the default URL's.
You would have to replace all occurrences of links with a function that checks if mod_rewrite is available, or more likely, a config value. It would then return the appropriate link.
getLink("?page=about-us")
Use an <IfModule> to avoid breaking other .htaccess directives and or 500 internal server errors if Apache doesn't understand your rules. Also add a single non-rewriting rewriterule (before all others);
<IfModule mod_rewrite.c>
RewriteEngine On
#The next rule does no rewriting, but sets en environmental variable.
RewriteRule .* - [E=RewriteCapable:On]
</IfModule>
In your file (store as setting or check on places generating/outputting urls):
if(isset($_SERVER['RewriteCapable'])){
//make fancy urls
} else {
//cludgy old-style urls
}
Hmm.. this may need thought about a bit to get the correct solution.. follow me here if you will :)
SEO URLs were primarily introduced to (1) include human readable text in the URLs and (2) to get rid of the GET parameters.
To look at point (2) for a moment, this was the primary driver initially, because people used about.php?id=1, id=2 ... id=3457348 to get the same page listed in the search engines multiple times, which of course got detected and stopped, then sometimes people would pass a session id=24234234 which would also get stopped as being a duplicate page (rightfully as it uses HTTP as a stateful protocol when it's not).
With an URL, everything from the first char up to a the # of a #fragment defines a resource (from an HTTP perspective), so rightly so when several different URLs all resolve to the same 'page' they are indeed duplicates.
So, by negating the GET parameters you solve this problem, which now isn't a problem by the way and hasn't been for a long time, there's no reason not to use GET params properly other than vanity.
So, really you solve no problem but have instead introduced a new problem, in that you want '/page/about-us' and '?page=about-us' to both go to the same 'page' which means you've got duplicate resources again and this could be detected and you could get penalised.
Thus, by introducing 'SEO URLs' you've actually created the problem SEO URLs were 'invented' to counteract.
This only leaves the point about human readable words in the URL. URLs are supposed to be transparent so they don't count for anything in reality, but some still like - so I'd have to ask what's wrong with using '/?/page/about-us'... and if you don't like that then whats wrong with creating a fixed file with the filesystem path '/page/about-us' which simply includes your index.php with the right variables set?
Of course you can create duplicate pages and have both SEO friendly urls and GET param URLs but as you can see that won't be SEO friendly now will it?
Something to chew on :)

REALLY basic mod_rewrite question

I am trying to use SEO-friendly URLs for a website. The website displays information in hierarchical manner. For instance, if my website was about cars I would want the URL 'http://example.com/ford' to show all Ford models stored in my 'cars' table. Then the URL 'http://example.com/ford/explorer' would show all the Ford Explorer Models (V-6, V-8, Premium, etc).
Now for the question:
Is mod_rewrite used to rewrite a query string style URL into a semantic URL, or the other way around? In other words, when I use JavaScript to set window.location=$URL, should the URL be the query string version 'http://example.com/page.php?type=ford&model=explorer' OR do I internally use 'http://example.com/ford/explorer' which then gives me access to the query string variables?
Hopefully, someone can see my confusion with this issue. For what it's worth, I do have unique 'slugs' made for all my top level categories (obviously, the site isn't about cars).
Thanks for all the help so far. I got the rewrite working but it is affecting other paths on the site (CSS, JavaScript, images). I using the correct path structure for all these includes (ie '/images/car.png' and '/css/main.css'). Do I have to use an absolute path ('http://example.com/css/main.css') for all files? Thanks!
Generally, people who use mod_rewrite use the terminology like this:
I want mod_rewrite to rewrite A to be B.
What this means is that any request from the outside world for page A gets rewritten to file B on the server.
You want the outside world to see URLs that look like
A) http://example.com/ford/explorer
but your web server wants them to look like
B) http://example.com/page.php?type=ford&model=explorer
I would say you want to rewrite (A) to look like (B), or you want to rewrite the semantic URL into a query string URL.
Since all the links on your page are clicked on by the user and/or requested by the browser, you want them to look like (A). This includes links that javascript uses in window.location. They can and should look like (A).
once you have set up mod_rewrite then your links should point to the mod_rewritten version of the URL (in your example: http://mysite.com/ford/explorer). Internally in your system you will still reference the variables as if they are traditional query string name value pairs though.
Its also worth pointing out that Google is now starting to advocate more logical URLs from a search engine point of view, i.e. a query string over mod rewrite
Does that mean I should avoid
rewriting dynamic URLs at all? That's
our recommendation, unless your
rewrites are limited to removing
unnecessary parameters, or you are
very diligent in removing all
parameters that could cause problems.
If you transform your dynamic URL to
make it look static you should be
aware that we might not be able to
interpret the information correctly in
all cases
http://googlewebmastercentral.blogspot.com/2008/09/dynamic-urls-vs-static-urls.html
also worth looking at:
http://googlewebmastercentral.blogspot.com/2009/08/optimize-your-crawling-indexing.html

Categories