URL rewriting test for beginners - apache

I was just learning to rewrite urls but got error on my first step.
My current url is:
http://localhost/tutorials/blog.php?blogId=30
.htaccess
RewriteEngine On
RewriteRule ^blogs/([0-9]+)/?$ blog.php?blogId=$1 [NC,L]
According to guide after this code my url should look like
http://localhost/tutorials/blogs/30/ but it's still same so where I am going wrong. Both .htaccess and blog.php are in same folder.

The code you have provides you an alternate way to work on URLs of the form http://localhost/tutorials/blogs/30/. It does not format the uglier URL to pretty/friendly one.
Use the following to achieve that:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /(.*blog)\.php\?blogId=(\d+) [NC]
RewriteRule ^ /%1s/%2/? [R=301,L,NC]
RewriteRule ^blogs/([0-9]+)/?$ blog.php?blogId=$1 [NC,L]

URL rewriting can be one of the best and quickest ways to improve the usability and search friendliness of your site. It can also be the source of near-unending misery and suffering. Definitely worth playing carefully with it - lots of testing is recommended. With great power comes great responsibility, and all that.
Refer this article https://www.addedbytes.com/articles/for-beginners/url-rewriting-for-beginners/.

Related

Mod Rewrite (SEO Friendly URL's)

You'd think I'd easily be able to find the answer to this on S/O, but I've tried everything and after a few hours of frustration I'm giving in and seeing what the real experts think.
I'm "sure" this can be done with mod rewrite, but I'll defer to you.
Problem: I'm attempting to turn a URL like this...
http://domain.com/new-cars/state.php?stateCode=al
Into this at minimum...
http://domain.com/new-cars/al-new-cars
Though, ideally I'd get it to look like this (yes, I'm willing to rewrite some code to use the full state name as the $stateCode variable to make it easier!)...
http://domain.com/new-cars/alabama-new-cars
Ultimately the plan is to be able to use URL's in links such as...
http://domain.com/new-cars/alabama-new-cars
And have .htaccess take car of associating this SEO-friendly URL with the dynamic version and displaying the page properly.
Either way, I haven't been able to figure out how to do this like I need.
Here's what I've tried.
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^new-cars/([^-]*)-new-cars/$ /new-cars/state.php?stateCode=$1 [L,QSA,NC]
And different variations that I've created using 2 different mod rewrite generators and various answers to other people's questions.
Absolutely nothing is working.
I expect when I go to
http://domain.com/new-cars/state.php?stateCode=AL
That it rewrites the URL to
http://domain.com/new-cars/AL-new-cars
...but it does not. Instead, it stays exactly the same dynamic URL I typed in. If I go to the "desired" rewrite URL I get a 404 error saying the page doesn't exist.
What am I doing wrong?
I thought maybe my .htaccess privileges weren't set right, but I can do a 301 redirect through .htaccess quite easily, so that's not it.
Maybe someone here can help. I've tried to so many permutations, even settling for the most basic rewrite just to see if I could get it to work - but nothing.
Any help is appreciated!
You can use:
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} \s/+new-cars/state\.php\?stateCode=([^\s&]+) [NC]
RewriteRule ^ /new-cars/%1-new-cars? [R=301,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^new-cars/([^-]*)-new-cars/?$ /new-cars/state.php?stateCode=$1 [L,QSA,NC]

Writing a RewriteRule with just one exception

For a project I need to write a RewriteRule in a .htaccess file but unfortunately I have nearly no experience how to write such rules. What I want to do is rather simple: A complete redirect from one path to another with just one exception. Let me show you my try:
RewriteCond %{REQUEST_URI} !^/success$
RewriteRule ^.*/wop_de/checkout/onepage.*$ http://%{HTTP_HOST}/wop_de/onestepcheckout/ [R=301,L]
What I have thought: If there is no string like "/success" in the URL do a redirect from "/wop_de/checkout/onepage/" to "/wop_de/onestepcheckout/". Well, I guess I was thinking the wrong way as it doesn't work. Could you help me, please?
Also do you know a good tutorial to learn how to write such rules? Thank you in advance!
Use this rule:
RewriteRule ^wop_de/checkout/onepage.*$ /wop_de/onestepcheckout/ [R=301,L]
.htaccess is per directory directive and Apache strips the current directory path (leading slash) from RewriteRule URI pattern.

URL: transform "/news.php?id=1" to "/news/1/"

I'm trying to make some more cleaner URL's for my website. I have allready found lots of questions regarding the removal of file extensions in URL's and managed to get that working. I want to take it a step further though by transforming an URL like http://www.site.com/news.php?id=1 into http://www.site.com/news/1/. I couldn't find an answer to this specific question so I'm asking it here. How could I achieve URL's like that?
Rewrite rules I allready have in my htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-]+)?$ $1.php
There are plenty of questions out there that answer your question. There are two things you have to do:
Redirect the ugly url to the fancy url, so that the user sees the fancy url
Internally rewrite the fancy url to the ugly url, so that the server can actually execute it.
You can find the documentation for mod_rewrite here. This solution uses the [END] flag which is only available from Apache 2.3.9 and up. You can find the documentation here. The trailing ? in the first rule discards the query string.
#Redirect ugly url to fancy url
RewriteCond %{QUERY_STRING} id=([^&]*)
RewriteRule ^news\.php$ news/%1/? [R]
#Internally rewrite fancy url to a usable url
RewriteRule ^news/([^/]*)/?$ news.php?id=$1 [END]

.htaccess remove part of url

I´m not feeling good asking this question (there are already a lot of good answers + tuts here). But after hours of trial I need help.
THE PROBLEM:
Due to an CMS Migration URLs have changed a bit. Old URLs are safed in a Database. Before I change ever link in Mysql I thought it would be much faster to do an .htaccess rewrite of those links. What I want is:
change
index.php/aktuelles?id=369:netzwerk-forst-und-holz-unterfranken-startet-ab-12-2012-&catid=1:news
to
index.php/369:netzwerk-forst-und-holz-unterfranken-startet-ab-12-2012-&catid=1:news
means simple cut the
aktuelles?id=
of the URL. Here my trials:
RewriteEngine On
Options +FollowSymLinks
RewriteRule ^aktuelles\?id\=$ $1 [R=301]
#RewriteRule (.*)/index.php/aktuelles?id=(.*) $1/$2 [R=301]
Thanks a lot for your help,
kind regards,
tony
Here you go. This should cut out the aktuelles?id= from the provided URL:
RewriteCond %{QUERY_STRING} ^id=([^]+]+)$
RewriteRule ^([^/]*)/(aktuelles)$ $1/%1? [L, R=301]
It's untested - please tell me if this worked for you.

Beginner's apache mod_rewrite assistance

I am not really familiar with apache mod_rewrite.
I have url parameters such as {domain}/index.php?blog=5
I simply want to make it {domain}/home.php?client=5
Is it a task as simple as it sounds and can anyone help?
The following might work, give it a try
RewriteCond %{REQUEST_URI} ^/home.php [NC]
RewriteCond %{QUERY_STRING} client=([0-9]+) [NC]
RewriteRule (.*) http://%{REMOTE_HOST}/index.php?blog=%1 [L]
That seems pretty simple, to be honest — once you get your head into mod_rewrite, it's not that complex.
It sounds like you want to add
RewriteEngine on
RewriteRule ^/index.php?blog=(.+)$ /home.php?client=$1
to your configuration.
Some caveats:
If you are putting this in a .htaccess file, then remove the / from the RewriteRule line.
If you want to make this case-insensitive, add [NC] to the end of that same line.
If you want users to see the URL change (so sending a 302 Found redirection to the browser), then add [R] to the end of the RewriteRule line.
If you want both a 302 Found and for the URL to be case-sensitive, combine the two instructions as [NC,R] at the end of the RewriteRule line.
It's definitely worth reading the mod_rewrite docs, but the rule above should be all you need for this use-case.