How to use RewriteRule with URLs like bit.ly? - apache

I don't know if they really using a RewriteRule (Apache mod_rewrite) for this, but if we append an URL after the URL of bit.ly (ie: http://bit.ly/http://www.somesite.com/), it takes the URL appended as a parameter (http://bit.ly/?u=http%3A%2F%2Fwww.somesite.com%2F).
Someone knows how to do that, maybe with a RewriteRule or something else? If so, what can be the regex to manage this?
Thanks!

I would imagine their rewrite rule looks something like this:
RewriteRule ^(http.*)$ ?u=$1?%{QUERY_STRING} [R]
Couldn't help but fiddle around with this: Accepts multiple protocols and avoids appending the ? if there is no query string:
RewriteCond %{QUERY_STRING} (^$)
RewriteRule ^((http|ftp).*)$ ?u=$1 [B,R,L]
RewriteRule ^((http|ftp).*)$ ?u=$1?%{QUERY_STRING} [B,R,L]

It's probably something in these lines:
<IfModule mod_rewrite.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
And then have index.php do a:
search database to see if link is ok or flagged (badware, spam, etc)
mark visit in stats database
throw http 301 headers on the user for a proper redirect

Related

Rewrite Rule - need guidance

I need to do some redirecting to get some internal links to work but I'm having a complete block.
The url would be http://www.something.com/faqs/What_happens_if_I_move_home?
redirected to http://www.something.com/faqs/index/What_happens_if_I_move_home?
but it must look like the original url. I'm sure there is a simple answer but rewrite rules and regex are a mystery to me at times.
I did try RewriteRule ^faqs(/.*)?$ /faqs/index$1 [R,L,NC]
amongst many others!
try this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/faqs/index
RewriteRule ^faqs/(.*) /faqs/index/$1 [L,NC]

mod_rewrite RewriteRule is not working always

how can i make the following condition work if there is no number, so it can handle www.example.com/en/fourthcategory as well as www.example.com/en/fourthcategory/8562
RewriteRule en/fourthcategory/([0-9]+) ?l=en&c=c4&p=$1 [L]
i could use two rules, but would prefer using only one, since in the end there would be double as much rules and there will be quite a lot of categories in at least four languages...
RewriteRule en/fourthcategory/([0-9]+) ?l=en&c=c4&p=$1 [L]
RewriteRule en/fourthcategory ?l=en&c=c4 [L]
or is this not the right approach to create RewriteRules for every possible category separately?
You can try this:
RewriteEngine On
RewriteRule en/fourthcategory/?([0-9]*) ?l=en&c=c4&p=$1 [L]
Be carefull, with http://www.example.com/en/fourthcategory/8562, you'll get a final URL like this:
/index.php?l=en&c=c4&p=8562
index.php can be any DirectoryIndex filename of your apache server.

mod_rewrite doesn't work both directions

I get an original url:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
with the following mod_rewrite code:
Options +FollowSymLinks
RewriteEngine on
RewriteRule ^menu/([^/]*)/([^/]*)\.html$ /menu/?pid=$1&alias=$2 [L]
I get this nice url:
www.mydomain.com/menu/29/pizza-hut.html
so, both url above reference the same page...right!
now the real deal is,
WHY when I type the url,the original one:
www.mydomain.com/menu/?myid=29&mypage=pizza-hut.html
it doesn't redirect to
www.mydomain.com/menu/29/pizza-hut.html
it keeps its original one in the address bar, is there any line I should add?
Essentially, all the links you generate should be the nice urls. Don't have links like /menu/?myid=29&mypage=pizza-hut.html in any of your pages. Use the clean URLs that you've ensured to route correctly on the back end that look like this: www.mydomain.com/menu/29/pizza-hut.html
The rules that you have rewrite on the server the nice looking urls to what your content understands (e.g. /menu/?myid=29&mypage=pizza-hut.html). That's the most important part. If you want to correct all the direct requests for the ugly URLs, you need to first make sure all your pages start using the nice looking ones, then you can maybe do something like this:
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2? [R=301,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /menu/?\?myid=([^&]+)&mypage=([^&\ ]+)
RewriteRule ^menu/?$ /menu/%1/%2.\html? [R=301,L]
Note the ? following the .\html

Simple mod_rewrite RewriteRule for messy legacy url's

Launching a new website for a new client. Their old site has about 50 products and unfortunately, the old product names do not match up to the new URL pattern
Old URL Examples:
example.com/products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription
example.com/products.aspx?category=Foo&product=ProductNameDescription&var1=1293.123
example.com/products.aspx?category=Bar&product=ProductCategoryProdNameRandomNumbers
(The old URL's are sometimes hitting 150+ characters.)
New URL's:
example.com/products/category/actual-product-name
There's no set, recognizable pattern to go from the old product name to the new one. There is for the category.
I've tried simple mod_alias Redirects, but understand that I need a RewriteRule instead. But I'm having problems. All I need is a 1-to-1 redirect for each of these 50 URL's. I thought I could do something like:
RewriteRule ^/products.aspx?category=Foo&product=ProductName
/products/category/new-product-name/ [R=301,NC]
But that isn't working. I know this should be simple, but I am stuck. Any ideas?
Use the pattern below for the rest of your redirect urls. Note that you escape special characters e.g. ? , . and space by adding a \ in front of them
RewriteEngine on
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /products\.aspx\?category=Foo&product=SuperLongNoBreakProductNameIDDescription [NC]
RewriteRule ^ /products/category/new-product-name/ [R=301,NC]
Have a look at the RewriteMap directive of mod_rewrite.
You can specify in a text file something like:
products.aspx?category=Foo&product=SuperLongNoBreakProductNameIDDescription /products/category/new-product-name
And in your httpd.conf
RewriteMap productmap /path/to/map/file.txt
RewriteRule ^(.*) ${productmap:$1} [R=301,NC]
Tip: If it's a permanent redirect you want, make sure you set an appropriate Cache-Control and Expires header to instruct browsers to cache the 301.
You can try something like this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^category=Foo&product=ProductName$
RewriteRule ^products\.aspx$ /products/category/new-product-name/? [R=301,L]
Notes:
In per-dir (.htaccess) context, the per-dir prefix is stripped, so you can't start the RewriteRule pattern with ^/.
You have to use RewriteCond to match against the query string.
As stated in another answer, a RewriteMap solution might be suited to this situation, if you have access to httpd.conf / the vhost definition for this site. I'm not sure how that works with query strings though.
For something like this, it might be a better solution to rewrite all of these URLs to a server side script, and use the script to do the HTTP redirect for each URL.

How do I make .htaccess do what I want? :) (appending query string to url)

Currently my .htaccess looks like this...
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php [L,QSA]
It currently changes any /xxx.php file into /xxx. This is great for SEO. However, I also want Mr. htaccess to convert certain URLs into a URL + query string. For instance when user goes to
/specific/somerandominfo
Then somerandominfo is passed to the specific.php file. I normally have no problem doing this using rewrites, but because of my fancy catchall rewrite, I can't figure out how to do it.
For example if I add
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
to my .htaccess, then hitting up /specific/somerandominfo just serves me a big fat 500 Internal Service Error.
Any help from you apache gurus out there would be so, so cool.
Thanks!
p.s. anybody want to also throw in any other cool SEO tricks that they like? I'll bake you cookies.
You are getting 500 error because your rules are creating an infinite cycle. Check apache error log to see if it is true. So you should design your rules properly. Maybe like that:
RewriteRule ^([^/]*)$ $1.php [L]
RewriteRule ^(.*)/(.*)$ $1.php?var=$2 [L]
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC]
This is mostly correct. I'd just add the B flag, like this:
RewriteRule ^specific/([^/]+)$ /specific.php?somerandominfo=$1 [NC,B]
This causes the capture group $1 to be properly escaped for use in query strings. Note that you can still use QSA to retain the query parameters used in the original request (in addition to somerandominfo).
Perhaps you'll want to post your actual RewriteRule.