SEO friendly URL not working using .htaccess - php

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

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.

how to rewrite html url as directory in htaccess

I want to rewrite all of my category urls as directories instead of long html pages. How do I do that? (The site is homewetbar.com if you need to look at the directory structure further to understand how it currently works.)
For Example:
First level category:
www.site.com/great-gift-ideas-c-35.html
I would like to display instead as:
www.site.com/great-gift-ideas-c-35
Second level category:
www.site.com/great-gift-ideas-gifts-recipient-c-35_85.html
I would like to display instead as:
www.site.com/great-gift-ideas/gifts-recipient-c-35_85
Third level category:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43.html
I would like to display instead as:
www.site.com/gifts-recipient/groomsmen-gifts-c-35_85_43
OR:
www.site.com/great-gift-ideas/gifts-recipient/groomsmen-gifts-c-35_85_43 (whichever is easier)
I think mod_rewrite is what you are looking for, here is a link with how to set it up and how to start rewriting your urls. It should have enough information for what you are looking for, for more advanced stuff you will want to know more about regular expressions, but your examples are simple enough.
RewriteEngine on
RewriteRule ^great-gift-ideas-c-35.html$ great-gift-ideas-c-35
this should work for your first example, the rest just replace out the appropriate urls.
You would need to rewrite all the href in your site to delete the ".html" part. Then, add this to your .htaccess
RewriteEngine on
RewriteCond ${REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1.html [L]
That would make any URL without the ".html" work as if it had it
Just check out this site: http://www.generateit.net/mod-rewrite/index.php it generate all you need
RewriteEngine On
RewriteRule ^([^/]*)$ /?example=$1 [L]
Changing example to what you need should do it

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]

Passing Variable in html extension pages. like html?a=asdfadf

I want to pass variable in .html extension pages and .html pages are made up of mod rewrite so these are not html files but php script which are made .html through mod rewrite.
any clue?
EDIT: My .htaccess file*:
RewriteEngine on
RewriteRule ^watch/(.*)/(.*).html$ video.php?tag=$1&video=$2 [L]
RewriteRule ^(.*)/([0-9]+).html$ index.php?tag=$1&page=$2 [L]
RewriteRule ^(.*).html?([a-zA-Z=]+)$ index.php?tag=$1 [L]
* As supplied in the comments. Spacing may be different than the original.
Add the [QSA] flag to your rewrites. That will take the original query string and add your new params to it.
RewriteRule ^(.*).html$ index.php?tag=$1 [L,QSA]
This will rewrite whatever.html?orderby=views into index.php?tag=whatever&orderby=views.
The only catch is, with PHP anyway, whatever.html?tag=somethingelse will give you some weirdness. ($_GET['tag'] will have two values, but only the one that shows up last will be the real value.) But that's generally fine; you just have to be sure not to provide URLs like that, and you can just not care what people trying those wacky URLs see. (Assuming, of course, that you validate $_GET['tag'] properly.)

Diggbar modrewrite- How do they pass URLs through modrewrite?

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.)

Categories