Forum Moderators: phranque
I am trying to do this with .htaccess. Please help me to correct my code.
1.Idea:
www.domain.com it should redirect to www.yahoo.com
2.Idea:
www.domin.com/3454 it should take it at the server level as www.domain.com/index.php?url=3454
I am very new to .htaccess. I was thinking of having a index.php file at the root and redirect it with "header(Location:)" when some one call www.domain.com but when some one call www.domain.com/some-thing I will redirect it as
www.domain.com/1234 to www.domain.com/domain?url=1234
Anyway, with .htaccess I manage to do this
Redirect 301 / [yahoo.com...]
Any help will be appreciated.
Thank you
Here it is again:
[webmasterworld.com...]
#RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} ^(www)*\.example\.com
RewriteRule (.*) [yahoo.com...] [R=301,L]
This will redirect anything after domain name as well, for example, http://example.com/about.html will go to yahoo.com/about.html. The R=301 issues a permanently moved header, the L means this is the last rule to process, ignoring anything that follows.
A note about this line:
#RewriteCond %{HTTP_HOST} .
The pound sign # is a comment, rendering it ineffective. Time and time again I've been told I need this line, but if I use it, it never works. :-) So if the above fails, remove the #.
2.
RewriteEngine On
rewrite_rule ^/([\w\-\d\_]+\)$ your-script.php?url=$
This is a little on the "loose" side as it accepts numbers, dashes, and underscores as well. If this is your intent, fine, you just need to remove the leading slash and correct the typo:
RewriteRule ^([\w\-\d\_]+\)$ your-script.php?url=$
Just make sure your-script.php properly filters the data in url.
If you just want digits as in your first example, a class is not needed:
RewriteRule ^(\d+)$ your-script.php?url=$
Well, this gave me a 500 internal server error page. This is part of a short url project.
Any thing after "http://domain.com/1234" domain that is "1234" should be redirected to "http://domain.com/domain.php?url=1234" [There is no WWW] so that I can perform a mysql query.
This code is giving me a 500 internal server error.
RewriteEngine On
RewriteCond %{HTTP_HOST}
RewriteCond %{HTTP_HOST} ^(www)*\.domain\.com
RewriteRule ^(\d+)$ domain.php?url=$
And this code is showing "Oops! This link appears to be broken."
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www)*\.domain\.in
RewriteRule ^(\d+)$ domain.php?url=$
Thank you.
# Technically, on should be lowercase:
RewriteEngine on
# ? = 0 or 1 and Rather than checking the HTTP_HOST on a separate line, you can
# check for the correct host or empty using a ?. Same with the www... the \. (dot)
# should be inside the grouping (), because if the www is not present there will
# not be a leading . (dot) either. Change TLD to your top-level domain.
RewriteCond %{HTTP_HOST} ^((www\.)?domain\.TLD)?$
# The preceding stated, you don't really need to check the host to do a rewrite
# or redirect if you have proper canonicalization in place in the file.
# The following did not work, because there is no back-reference defined. () = a
# Back-reference / grouping definition, and you count the ( left parenthesis to
# know what number to use. In this case it's 1.
RewriteRule ^([0-9]+)$ domain.php?url=$1 [L]
# [L] = Last and should always be used, unless you know you don't need it and
# exactly why you don't need it.
# The preceding / on the left side of the rule RewriteRule ^/ generally
# MUST be used in the httpd.conf file, and MUST be omitted from the .htaccess, so
# depending on where you put your code you may or may not need the preceding / on
# the left side of the rule.
# I switched from \d to 0-9, because Mod_Rewrite runs on a different regex engine
# than PHP or Perl and I do not remember if it accepts the \d as a 'digit'
# denotation, and off the top of my head I think it does not... I have not used \d
# in an .htaccess or httpd.conf file personally.
# I HIGHLY recommend before using any mod_rewrite you take the time to learn
# and understand EXACTLY what everything you are using does, because if you
# make a mistake in here you can not only break your site, you can break it in
# such a way it could go unnoticed by you until you've decimated your search
# engine rankings and it's too late to do anything except fix it and wait...
# This is not PHP you're playing with, and if you make a mistake it can be very
# costly... Not only with search engines, but you can actually wear your server
# out early in some cases. Please see the Apache Forum [webmasterworld.com] and do your homework.
# before you play around with Mod_Rewrite.
Since my application accepts number as well as alphabets, I changed
RewriteRule ^([0-9]+)$ domain.php?url=$1 [L]
to
RewriteRule ^([\w\-\d\_]+)$ domain.php?url=$1 [L]
and it perfectly works. Thank you.
I am curious to know from where do I learn all this? I made a lot of search on google but the results are about some thing else. May be I dont know what these things are exactly called.
I am curious to know from where do I learn all this?
The Apache Forum [webmasterworld.com] (many thanks to jdMorgan for all the great posts) the Mod_Rewrite Documentation [httpd.apache.org] and personal use...
You can save a few characters by changing your pattern to:
RewriteRule ^([\w\d_-]+)$ domain.php?url=$1 [L]
You don't need to escape the underscore at all, and if the hyphen is at the end of the pattern it does not need to be escaped either.
We just had a thread in here a few days ago where PCRE patterns failed, but POSIX patterns worked fine after a change of servers. PERL-compatible regular expression support is realtively new, and must be explicitly enabled. If you have to move back to an older server, or if someone forgets to enable PCRE support, your code could stop working...
Jim
Jim