Forum Home
Press F1
 
Thread ID: 121647 2011-11-04 06:18:00 Help with regular expressions bot (15449) Press F1
Post ID Timestamp Content User
1241776 2011-11-04 06:18:00 Currently, to view a webpage on my website, the user must go to mywebsite.com (not my actual domain :p). What I want is a RewriteRule in my .htaccess file that replaces mywebsite.com with mywebsite.com (mypage being replaced with the page ID). Is there a set of regular expressions to do this? I found an example online, but it only replaces numbers, not strings. bot (15449)
1241777 2011-11-04 06:42:00 You mean a redirect? I have read the 301 is the preferred method for search engines, in that you don't dilute or lose any rankings in search results. Procedure here. (www.isitebuild.com) Then I think you need specify to Google the canonical or preferred version (googlewebmastercentral.blogspot.com) of your url for duplicate content - this I believe reduces the spread of any page rank across similar url's. Might pay to sign up to webmaster tools, (accounts.google.com) where Google have recently added a section for URL's in the diagnostic section and how they might assign a preferred url (www.google.com) if not already specified by the webmaster. kahawai chaser (3545)
1241778 2011-11-05 04:21:00 Got it working -- perhaps Erayd could check this over?

RewriteRule ^([A-Za-z0-9]+)/?$ somepage.php?id=$1

What this does is, when the user goes to mywebsite.com it's really going to mywebsite.com
bot (15449)
1241779 2011-11-05 04:39:00 You mean a redirect?Nope, he means URL rewriting - it's not the same thing.


Got it working -- perhaps Erayd could check this over?Sure.


RewriteRule ^([A-Za-z0-9]+)/?$ somepage.php?id=$1
Unless you don't intend to use the query string for anything else, you should be using the QSA flag - otherwise it'll drop the rest of your query string when the rule is applied.

Depending on the way your server is set up, you may also need to prepend /? to that expression (after the caret), otherwise it won't catch things where the evaluated URL is something like /home. This isn't always necessary, but adding it can't cause any problems, and will potentially solve a few.

You should also remove the $ from the end of it, unless you only want to capture things like /home and not /home/blue/fish. Depending on how much you care about the blue fish, you may also wish to capture that and do something with it (although you don't necessarily need to do that here, as it's also available from PHP via $_SERVER['REQUEST_URI']).

If you want to learn a bit more about regex, this site (http://www.regular-expressions.info/) is excellent.
Erayd (23)
1