Diggbar modrewrite- How do they pass URLs through modrewrite? - php

With the new Diggbar, you can put http://digg.com in front of any URL that you are currently at and it will create a Digg short URL. I am only assuming they do this by modrewrite (though I am not sure since I am new at this all).
How is this done? It seems to me when I try this with a website I am working on, it bombs out.
I want to be able to do the following:
http://example.com/http://stackoverflow.com/question/ask
and have a modrewrite that will allow this to go to
http://example.com/index.php?url=http://stackoverflow.com/question/ask
But when I use this modrewrite:
RewriteEngine on
RewriteRule ^([A-Za-z0-9]+)$ /message.php?id=$1 [L]
it doesn't work. What am I doing wrong?

You have to take the value from the request line because Apache removes empty path segments. The initially requested URI path /http://foobar/ becomes /http:/foobar/. But the request line (THE_REQUEST) stays untouched:
RewriteCond %{THE_REQUEST} ^GET\ /(https?://[^\s]+)
RewriteRule ^https?:/ index.php?url=%1 [L]

You're only looking for letters and numbers in that regular expression, so it won't pick up the colon and slashes. You're also using index.php in the example and message.php in the htaccess ;)
You'll probably want something like this:
RewriteEngine on
RewriteRule ^http://(.+)$ /index.php?url=$1 [L]
This makes sure you only catch URLs here, and you can still have regular pages! (Think about what would have happened if you went to example.com/index.php, you'd end up in an infinite loop.)

Related

.htaccess URL rewrite ignored

.htaccess newbie here.
I have a URL like this:
example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6
that I need to be rewritten like this:
example.com/lesson-plans/earth-sciences/some-title-6
I am using the following .htaccess URL rewrite:
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/([^/]*)/([^/]*)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
However, when I hover over/click on links of the original format (example.com/lesson-plans/earth-sciences/show_lesson_plan.php?title=Some_Title&id=6), they are not being rewritten. I've tried a few different rewrite rules, but nothing works.
How can I make this rewrite work? As far as I know, .htaccess is working on my server and rewrites are permitted.
You were close
RewriteEngine on
RewriteCond %{QUERY_STRING} title=([^&]+)&id=([^&]+)
RewriteRule ^lesson-plans/earth-sciences/show_lesson_plan.php$ /lesson-plans/earth-sciences/%1-%2 [QSA,L,R]
Randomly while lying in bed last night.
You have the rewrite rule back to front. you have to add the rule for the rewritten url to turn it back into an ugly one
see: http://martinmelin.se/rewrite-rule-tester/
RewriteEngine On
RewriteRule ^lesson-plans/earth-sciences/(.*)-(.+)$ /lesson-plans/earth-sciences/show_lesson_plan.php?title=$1&id=$2&cat=3 [L]
so
lesson-plans/earth-sciences/some-title-6
becomes
/lesson-plans/earth-sciences/show_lesson_plan.php?title=some-title&id=6&cat=3
I ended up using the following code and am posting it as an answer in the event someone else finds this useful:
RewriteEngine on
RewriteRule ^([^/.]+)/([^/.]+)$
show_lesson_plan.php?title=$1&id=$2
This will take a url (like this: http://example.com/lesson-plans/earth-sciences/a-cloud-in-a-bottle-i/8923) and run it through this: http://example.com/lesson-plans/show_lesson_plan.php?title=$1&id=$2
Note that I changed the original URL slightly, opting to break the id out of the title string.

SEO friendly URL not working using .htaccess

I'm trying to implement a SEO friendly URL using .htaccess by using the RewriteRule below
RewriteRule ^n/article/([a-zA-Z0-9]+)/$ article.php?title=$1
The actaul URL looks like this
http://localhost/n/article.php?title=this-is-the-first-news-article
but I want it to look like this :
http://localohst/n/article/this-is-the-first-news-article
When I applied the RewiteRule above it does not change to the desired URL
This should do it. You are missing the n. Not sure why you need the word title though.
RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1
You have to capture the query string first. You can't do that with a RewriteRule because they ignore the query string. Here we're using [R] to redirect. If this is working for you and there is the potential that the old URLs are being stored somewhere as links, then you may want to specify [R=301]. Be sure to remove all old-style links from your site though (that contain the previous link format we're rewriting), that way you're not penalized for not updating your links.
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} title=([^&]+)
RewriteRule ^n/article.php n/article/%1? [R,L]
If your site needs the formatting to function from the original, you might also need this after the first rule. This rule quietly redirects the URL back to the original without showing it to the end user:
RewriteRule ^n/article/(.*) n/article.php?title=$1 [L]
it will be RewriteRule ^n/article/title/([a-zA-Z0-9]+)/$ article.php?title=$1

URL Rewrites not working

On my website I am trying to rewrite a long URL to a SEO friendly one.
I've got the following code, but it doesnt seem to affect anything! However if I type dgadgdfsg into my htaccess, it throws an internal server error. So I am presuming it is something with Rewrite Rule.
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
I have confirmed that mod_rewrite is on.
This is the current URL
http://mysite.com/missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
and this is what I want it too appear like
http://mysite.com/1/Liam/Gallagher
Change your RewriteRule to this (slightly modified from your version)
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [QSA,L]
If that doesn't work try putting a R flag for testing purpose (which will make your browser change the original URI to: /missing-people/user-profile.php?userID=1&firstName=Liam&lastName=Gallagher
Presuming your userID is comprised only of digits and firstName and lastName are only alphanumeric.
RewriteEngine On
RewriteRule /(\d+)/(\w+)/(\w+)/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]
A more strict version that does the same thing except it sets boundaries for the beginning and the end of the evaluated regex.
RewriteEngine On
RewriteRule /^(\d+)\/(\w+)\/(\w+)$/ /missing-people/user-profile.php?userID=$1&firstName=$2&lastName=$3 [L]

mod rewrite - second rule is not working

I am trying to learn url rewriting.
My code:
RewriteEngine on
RewriteRule ^/$ /index.php
RewriteRule ^/([a-z]+)$ /index.php?page=$1
When I try it like this: localhost/mysite it shows home page. But when I try something like this: localhost/mysite/abcdefg, it would show a 404 error.
EDIT
What I want to do is:
If only original domain is given, it should goto home page. Eg: www.mysite.com --> www.mysite.com/index.php. Otherwise, if www.mysite.com/contactus --> www.mysite.com/index.php?page=contactus
EDIT
I am using WAMP server in Windows XP.
That's because the first rule would catch the second request. Now, that I took a closer look at the regex, no it would not catch the request. However, your second request would fail. Also, as a rule of thumb the more specialized a rewrite is the higher it should be placed.
You don't need to rewrite all the requests to index, but if you know what you are doing, then re-order the rewrites.
RewriteEngine on
RewriteRule ^/([^/]+)/$ index.php?page=$1 [L]
RewriteRule ^/$ index.php [L]
Edit 1: Taking into account that you are working on a localhost, this would work for you.
RewriteEngine on
RewriteBase /mysite/
RewriteRule ^([^/]+)/?$ mysite/index.php?page=$1 [L]
RewriteRule ^$ mysite/index.php [L]
When you go live, just remove the mysite/ part.
Note: You don't need this rule RewriteRule ^$ /index.php [L] the server will automatically load index.php if you visit localhost/mysite. That is the expected behavior if your server is configured to load a default page, the file index.php, on httpd.conf configuration file.
Edit 2: I see your edit, but you can't test that rewrite in the current URL structure you have in the localhost. You should try and setup virtual hosts to test in an environment that resembles your production as much as possible. Search on Google for how to create virtual hosts for your WAMP, XAMPP, or any other stack you are using.
Then the rewrite rules are simple
RewriteEngine on
# page-url -> index.php?page=page-url
RewriteRule ^([^/]+)/?$ index.php?page=$1 [L]
/localhost/mysite/abcdefg will not match the rule you expect it to match because the path (/mysite/abcdefg) contains a / in the middle that is not matched by your regular expression. So the web server looks for the file, can't find it, and returns a 404.
RewriteRule ^/([a-z]+)$ /index.php?page=$1
will match any string beginning with/ followed by any number of characters a-z. / is not in that range, that is why it fails on /mysite/abcdefg.
#trott and #anders_lindahl are right:
Your first only matches localhost/ aka the root of the site.
The second rule will match anything that has lowercase letters (and just that!) after the first slash, so localhost/thisisavalidstring.
You have put a / in there, so it will not match. use something like:
RewriteRule ^/([a-z\/]+)$ /index.php?page=$1
(haven't tried it, but I assume it will work. I'm not too sure about the need to escape inside the [])
You probably meant to use
RewriteRule ^/([a-z/]+)$ /index.php?page=$1
or
RewriteRule ^/([a-z/]*/)?([a-z]+)$ /index.php?page=$2

Pretty URL’s and mod_rewrite

I can’t seem to make this work - and I’m pretty sure that the real problem is that I just starring myself blind on it, so I hope a pair of fresh eyes can help me out.
What I wan’t to do is have several applications attached to my system.
At this time, a website already exists in the root folder, but I wan’t some microsites/powerformats in a CI installation.
My mod_rewrite looks like this:
RewriteCond %{REQUEST_URI} ^/(powerformat1|powerformat2)/?$
RewriteRule ^(.*)$ powerformats/index.php/$1 [L]
Although, getting the CI index.php correctly, when trying to access example.org/powerformat1 or example.org/powerformat2 gives me CI’s 404 page.
It seems like whatever I try of rewrite rules I either get the 404 page or nothing at all.
Any insights?
-- EDIT --
What I believe is my problem is that CI actually gets the 'powerformat1' string passed as the first segment. That is what I need to avoid. But can't that be solved through mod_rewrite?
You could try link directly to the file with the appropriate query string instead
RewriteRule ^(.*)$ /powerformats/index.php?somequery=$1 [L]
(you may have to change the slashes, see below)
Or it may be this:
Accessing /powerformat1/
may be rewriting to
powerformats/index.php//powerformat1/
You could try
RewriteRule ^/(.*)/$ /powerformats/index.php/$1 [L]
or some other variation with slashes:
RewriteRule ^/(.*)/$ /powerformats/index.php/$1/ [L]
Did you miss RewriteEngine On ?

Categories