homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 117 message thread spans 4 pages: 117 ( [1] 2 3 4 > >     
URL rewrite help needed

 12:52 pm on Jul 23, 2013 (gmt 0)

Hi this is my first post on this forum.
The content of this forum is great,so keep up guys!

I have a problem with url rewriting.

This is my test page that I'm using for this purpose:

I want to rewrite url:




or to be more precise to rewrite any page that contains
variable name.

Content of .htaccess file is:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1

The problem is that url rewrite doesn't change in address bar it stays the same when I access to it by link on my navigation bar the link is:

<li><a href="index.php?page=about">About</a></li>

Adress bar:

But when I type manualy in address bar


it opens page correctly

So where is my mistake?
Any help guys?I would appreciate it!Tnx in advance!

[edited by: phranque at 2:24 pm (utc) on Jul 23, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]



 8:26 pm on Jul 23, 2013 (gmt 0)

Hi nevski,

Welcome to WebmasterWorld!

What you're asking for is a bit more complicated than a simple "rewrite", because if all you do is "rewrite" the index.php?var=val to /val you'll have duplicate content since both index.php?var=val and /val are accessible externally.

What you'll need to do is: Redirect /index.php?var=val to /val for External Requests only (this means you'll need to use %{THE_REQUEST}), then after you redirect you'll need to rewrite /val back to index.php?var=val so the information generated by index.php shows on /val.

The best place to start is in the Library [webmasterworld.com] at the bottom of the page. There are a few tutorials that explain the differences in rewriting v. redirecting and regular expression use in way more detail than I have time to right now.

When you have a better understanding, set up a test site and try just rewriting from /val to index.php?var=val in the .htaccess of a test-domain or test-subdomain so you don't break your site. (Don't worry about the redirection yet, just get a feel for rewriting and how mod_rewrite works.)

If you get stuck (chances are you will) post what you have and we'll give you a hand figuring out the issues, then when that's working, take a shot at the redirecting and, again, when you get stuck (very very good chance you will) post what you have again and we'll help you get it working.

One of the reasons we don't code for other people is .htaccess solutions are usually unique, they can break your site and if you don't understand everything going on it your .htaccess file and why it's going on it can cause some really big issues, because although a 500 server error sucks it's obvious, but there are things that can happen "silently" with mod_rewrite (like content duplication) that might go unnoticed until they "bite you" one day.

* Make sure you empty your cache between every rule change/refresh, because it's not uncommon to get the results of a previous version of an .htaccess file on a refresh when you don't have an empty cache.


 9:37 pm on Jul 23, 2013 (gmt 0)


Tnx you for your response and the effort I appreciate it!

Yes it's harder than I thought,so I will start from scratch by your guidlines!Once again tnx for profound post!


 10:00 pm on Jul 23, 2013 (gmt 0)

NP, glad I help!


 10:23 pm on Jul 23, 2013 (gmt 0)

welcome to WebmasterWorld, nevski!


 10:47 pm on Jul 23, 2013 (gmt 0)

I want to rewrite url:




No, you don't. Really. What you want is to

#1 redirect
(browser's address bar shows shorter prettier URL instead of longer uglier form)

and then

#2 rewrite
to fetch content from


And make sure that any links on your own site point to the URL you want people to see and use. Uh-oh, got a nasty feeling I'm stealing g1smd's boilerplate Never link to something that will be redirected.


 5:08 pm on Jul 24, 2013 (gmt 0)


You had a point that url rewriting is not piece of cake like I thought or to be more precise like I read.So I began from scrach reading Library step by step and trying to figure it out.It will surely take some time,but that's I guess the only way.

I've made also two page test site to see how redirecting works

RewriteEngine On
RewriteRule ^test\.html$ http://www.example.com/ [R=301]

It worked and I'm glad because I've started to lose all hope in progress)))

So if I can ask you one thing to write simple code example


url redirect and rewrite of one variabile

for example example.com/index?page=about
to be redirected to example.com/about

The purpose of it is to see and examin every element of code,and on that base to learn how things go..It would be of great use to me...Tnx guys in advance!

[edited by: phranque at 7:50 am (utc) on Jul 26, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]


 7:05 pm on Jul 24, 2013 (gmt 0)

Yeah, it's not quite as simple as people who know say, but once you get your head wrapped around it, it's not too bad.

Your redirect looks good! Glad you got the status code R=301, because the "default" is a 302 and there aren't too many any more, but in the past Search Engines had issues with them, so telling them "what the redirect means" is a better way to go.

The only "adjustment" I made is to add the L flag so the file stops processing once the rule matches... You should always use the L flag unless you know why you don't need/want to.

RewriteRule ^test\.html$ http://google.com/ [R=301,L]

I'm about to take off, but I'll see if I can get back to you later on your example, but in case I forget or something, here's a couple of threads that cover quite a bit:



 1:22 am on Jul 25, 2013 (gmt 0)

Good answers so far. This topic is "simple" when you break it down to basics. The complicated bit is the actual code to implement it.

Just to clarify one thing: the rewriting process doesn't make or change any URLs! This is a common misconception.

When you have a site using http://example.com/index.php?page=about but you want instead to use http://example.com/about there are three steps to follow.

1. Change the links on the pages of your site so that the href value is in the new format that you want users to see and use. Everything begins with the click that generates a URL request.

2. Install a RewriteCond/RewriteRule pair that looks for external requests for the old URL format and then tells the browser to make a new request for the new URL format. This is an external redirect. Make sure it's a 301. The RewriteCond should test THE_REQUEST and it stops the possibility of an infinte redirect loop.

3. Set up a RewriteRule such that when a new format URL is requested, the request is mapped to the right place inside the server to fulfil the request. This is an internal rewrite.

Get step 3 working first. Then do step 1. Step 2 is optional, but removes the possibility of duplicate content.

You need to be completely clear as to the difference between a redirect and a rewrite, and be happy that a RewriteRule can be configured to perform either of those functions.

Additionally, once you start using RewriteRule on your site, you must use it for all of your rules. This means you must stop using Redirect and RedirectMatch rules and forget that those older directives ever existed.


 11:14 pm on Jul 25, 2013 (gmt 0)

Hi guys,tnx for replies,I spent my day in reading about mod_rewrite,syntax and site Library.Tried some simple urls,but I miss always something.

I've tried to make like simplest testing site on my localhost.

It's some basic html.

rewriting and redirecting from dog.html---->>cat.html

RewriteEngine On
RewriteRule ^dog.html$ cat.html

So I have few question to see if I'am on right path.

This rule that I wrote is redirect,right?
When I click on dog.html it redirects me on cat.html page.
What would be a difference between that code and if I wrote
same code with [L,R=301] beside it?For L I thing I got it from previous posts.

And what about if I would like to rewrite url dog.html to dog without .html what kind of rule I must apply if I want to see somesite/dog without.html on the adress bar.That confuses me more than anything?)))Tnx once again.


 12:27 am on Jul 26, 2013 (gmt 0)

This rule that I wrote is redirect,right?

Wrong :(

The easiest way to tell the difference between redirect and rewrite from the end user's POV is to look at the browser's address bar.

RewriteRule ^dog.html$ cat.html

= address bar continues to say example.com/dog.html but browser displays content of page example.com/cat.html

RewriteRule ^dog.html$ http://www.example.com/cat.html
RewriteRule ^dog.html cat.html [R]

= address bar changes to say example.com/cat.html AND browser displays content of cat.html

In real life, the two forms would be

(external redirect)
RewriteRule ^dog.html$ http://www.example.com/cat.html [R=301,L]

(internal rewrite)
RewriteRule ^dog.html$ /cat.html [L]

Note the details:
Rules creating a redirect go before rules creating a rewrite
Every rule includes the [L] flag unless there's a specific reason for omitting it, OR there's some other flag such as [F] or [G] that carries an implied [L].
Redirects are flagged R=301 unless you specifically want it to be a termporary 302 (the default in both mod_rewrite and mod_alias).
Targets of internal rewrites have a leading / slash (this is a security measure which g1smd will explain).

:: shuffling papers ::

I haven't posted the rewrite/redirect boilerplate in a while, so I'll include it here.

The Redirect-to-Rewrite Two-Step

Problem: Your dynamically generated pages have long, ugly, hard-to-memorize URLs, probably containing query strings. You want them to have short pretty URLs.

The Solution comes in two parts.

Part 1. Redirect
When a user asks for the long ugly URL, redirect to the short pretty URL. Basic pattern:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} queryname=([a-z]+)
RewriteRule longcomplicatedURL http://www.example.com/blahblah/%1? [R=301,L]

The %1 is captured from the original query string, and the final ? means that you now get rid of the query string. In real life it will usually be a little more complicated, but that's the basic process.

user asks for

They get bounced over to

Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ /longcomplicatedURL?queryname=$1 [L]

This time around, you're capturing part of the request and changing it into a query string.

user asks for

They may think that's what they're getting-- it's what the browser's address bar says-- but behind the scenes the page content is really coming from

Now you see why Part 1 had to look at THE_REQUEST. It's for insurance. If something happens later on, your long complicated URL might pass through mod_rewrite again. If it does, you need to be sure it doesn't get re-redirected. Otherwise there will be an infinite loop.

Now wait a minute! Does this mean that if someone starts out asking for "longcomplicatedURL", they go through this whole rigamarole and then they end up right back where they started?

Yup. But they don't know it. They only know what the browser's address bar tells them. Even robots-- yes, even google-- can't tell that they're being rewritten.

The Redirect part of the package-- Part 1-- is not technically necessary. The Rewrite-- Part 2-- will function without it. But redirecting everyone to the same URL means that everyone is now on the same page ... and it avoids nasty things like Duplicate Content.

But you're not done yet.

Part 0.
Before you do anything with Part 1 and Part 2, go over your current site carefully. Make sure that your own links point only to the short pretty URL. Requests for the long complicated URL should come only from outside-- from people with outdated bookmarks, or old links from other sites. Your own site will use only the pretty URLs.


 1:48 am on Jul 26, 2013 (gmt 0)


Thanks your post is really helpful for me))

I read your whole post and thread rewrite-redirect whit that same text.I was trying to implement this code.Redirect works. I get in address bar location that I've asked by url redirect but also 404error.

It would be great for me to see the mod_rewrite finally working.Becuase I want to analyze the code piece by piece.I don't see any use in just copy-paste of the code and in short therm to resolve the issue when in long term the knowledge can be of great use.Just it would be great to see the correct code and examine it.

So my goal is to rewrite and redirect(i'm using localhost)




I've used this code and set these paramaters:

RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=about
RewriteRule ^index\.php$ localhost/test-page/%1? [R=301,L]
RewriteRule test-page/about$ index.php?page=$1 [L]

When I click on the link,no matter home or about or contact it shows:

http://localhost/test-page/ on address bar
and 404error

Where i did wrong?
Tnx in advance!


 2:49 am on Jul 26, 2013 (gmt 0)

I want to analyze the code piece by piece. I don't see any use in just copy-paste of the code and in short therm to resolve the issue when in long term the knowledge can be of great use

Super :) This kind of thinking makes everybody happy. In fact I have another piece of boilerplate called "why we make you do it yourself" but I hardly ever post it because people tend to get annoyed.

Here's another detail: Always include a blank line after each RewriteRule. This is not for server reasons. It is just to keep you oriented and help you see where one rule ends and another one starts.

When I click on the link,no matter home or about or contact

I have suspected all along that the problem is not in your RewriteRule but in the page itself.

Now, even without using LiveHeaders or similar, you can see where a link will lead before you click on it. If you hover over the link, your browser will show the URL somewhere-- maybe in a tooltip-type popup box, maybe in a message area at the bottom of the window. Somewhere, anyway. You can also use View Source to see what the <a href... says. Note here that if your pages contain any php or other dynamic content, then what you see in "View Source" will be different from what your original php file (the physical file on the server) said.

Existing rules:
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=about
RewriteRule ^index\.php$ localhost/test-page/%1? [R=301,L]

RewriteRule test-page/about$ index.php?page=$1 [L]

There are two obvious problems in the rules. Or the same problem twice. Forms like $1 and %1 mean "insert text that was previously captured" --but you're not capturing anything, because there are no parentheses.

In the first rule, the redirect, the final Condition has to say
RewriteCond %{QUERY_STRING} page=(about)
with parentheses so you will capture "about". Later, of course, you will change this to something like
so you can capture more than one possible request. If the only word you ever want to capture is "about" then you wouldn't need %1 in the target; you could just write out "about" again.

And in the second rule, the rewrite,
and so on, exactly as above.

With the rules as written, the results would be:

User requests

Server rewrites to serve content from
where the expected value for "page" is empty. If the php page is set up properly, this will indeed return a 404 response, because the php hasn't been able to build a page.

And conversely:

User requests

Server redirects to
... and that's all, because the value "about" in the query string wasn't captured. (Note that if the condition said "page=([^&]*)" and the request was for "index.php?page=" with nothing more, the same thing would happen.)

Does your RewriteRule really say
localhost/ et cetera
in the target? I would expect that to take me to
http://localhost:8888/localhost/ et cetera
but possibly MAMP and WAMP don't behave exactly the same in this respect.


 2:50 am on Jul 26, 2013 (gmt 0)

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=about
RewriteRule ^index\.php$ localhost/test-page/%1? [R=301,L]
RewriteRule test-page/about$ index.php?page=$1 [L]

Ah, you're not "capturing" a pattern in the condition to use %1 with, so it's empty... You're also redirecting all requests rather than only external requests.

So... Let's just "remember" the info we need from external requests only and Redirect those, but eliminate internal requests for the resource so we can Rewrite to those:

# Match any external request if the request begins with
# anything that's not a literal ? one or more times, and
# is then followed by a ?, followed by page=, followed by
# any letter, number, underscore or hyphen
# [() = grouping + store for back-reference]
# followed by [space] HTTP ...
# Redirect that request to /test-page/ whatever page=
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ^[^?]+\?page=([a-z0-9_-]+)\ HTTP [NC]
RewriteRule ^index\.php$ /localhost/test-page/%1? [R=301,L]

# Match anything that starts with test-page, and is followed by a /,
# then followed by any letter, number, underscore or hyphen and
# Rewrite to /index.php?page=[any letter, number, underscore or hyphen]
RewriteRule ^test-page/([a-z0-9_-]+)$ /index.php?page=$1 [NC,L]

* Note I'm coding off the top of my head and might have an error or two, but it should be close.

** Notice the minor adjustments I made in bold.


 3:52 am on Jul 26, 2013 (gmt 0)


[^?\ ]*
So that
#1 rule will still work if somehow the request comes in as
example.com/?blahblah (literal question mark)
#2 rule cuts out a little sooner, with no backtracking, if there doesn't happen to be a query at all


Can be constrained to
since the rule is meant for a narrowly delimited set of pages, all of whose names are composed entirely of lower-case letters. No need for [NC], since a request in the wrong case can only come from deliberately wrong input, and those don't deserve anything but a 404.

[NC] in the "rewrite" part of the rule is only appropriate if the php page code is expanded to include a redirect (not rewrite) to the correctly cased form of the directory name. If the case-neutrality is intended to apply only to the requested page name, it should be expressed as [A-Za-z] without flag. And, again, the php will have to either redirect or issue a 404. (Site owner's choice.) Silently correcting the case is not the best option.


 4:32 am on Jul 26, 2013 (gmt 0)

Good points Lucy24!

I'm a bit out of practice "coding off the top of my head" for people, so I appreciate you taking the time to make "suggestions/corrections" when you see them.


 5:56 am on Jul 26, 2013 (gmt 0)

One you did url rewrite you simply use 301 redirect case.


 7:53 am on Jul 26, 2013 (gmt 0)

I'm a bit out of practice "coding off the top of my head" for people

Well, it's a ### of a lot easier to find imperfections in other people's code than to write perfect code for yourself. Which is why message boards are most useful when there are at least two people answering each question.


 8:56 am on Jul 26, 2013 (gmt 0)

welcome to WebmasterWorld, meenuweb!

which question are you answering?


 10:39 am on Jul 26, 2013 (gmt 0)

Well, it's a ### of a lot easier to find imperfections in other people's code than to write perfect code for yourself.

No kidding... That's why I appreciate the corrections/suggestions! I try to "get it right" for the situation when I post, but when I miss, it's cool to have someone else be able to point out where.


 12:42 pm on Jul 26, 2013 (gmt 0)

Guys you are great I'm finaly starting to understand things better because of your dedication.

I did some changes to code that I written

so the code looks like this:

RewriteEngine On

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=(about)
RewriteRule ^index\.php$ /localhost/test-page/about [R=301,L]
RewriteRule test-page/about$ index.php?page=(about) [L]

The result on my browser address bar shows error:


when I hover over link at testing page,the link doesn't change

it is stil localhost/testing/index.php?page=about

Also this rule doens't affect to other pages they load normal.
So from what I got it rewrites place where index.php was to
with this

RewriteRule ^index\.php$ /localhost/test-page/about$ [R=301,L]
RewriteRule test-page/about$ index.php?page=(about)[L]

Tnx guys in advance!


 1:16 pm on Jul 26, 2013 (gmt 0)

RewriteRule test-page/about$ index.php?page=(about) [L]


#1 If you're going to put literal text in a target, you don't need to capture anything anyway
#2 and far more lethally: You've got the parentheses in the wrong place. You can only capture from the pattern. Any parentheses in the target will come through as literal parentheses, meaning you're redirecting yourself to a page with query

when I hover over link at testing page,the link doesn't change

"What we have here is a failure to communicate" :)

You're looking in the wrong place. But I can't give details because not all browsers are alike. In the two browsers I've used most recently: If I hover over any link, the URL for that link will show up along the bottom of my browser window. This is probably the most common way to show an URL, but just watch. Some browser will do it differently.

it is stil localhost/testing/index.php?page=about

Now, wait. Your link isn't supposed to say that anyway. It's supposed to say /testing/about That is, your links should point directly to the URL you want people to see and use. The redirect is only for insurance, in case someone types in the wrong URL.

The result on my browser address bar shows error:

http:/ /localhost:1337/localhost/test-page/about?page=about

Yes, I kinda thought it would do that. Leave off the "localhost" element in the target, and start with the slash. On your live site, redirect targets should begin with
but on MAMP/WAMP they can't. Well, technically they could say
but there's no reason to do that. (1337? Really? Mine's 8888.)


 2:10 pm on Jul 26, 2013 (gmt 0)

Tnx Lucy24 for helping,

To get to more colpex codes fist of all I must learn the basic is code.I guess it needs time to learn mod_rewrite,it can't be done in week period or so.Especially becuase I want to learn it good.To know syntax and the use of everything.Becuase if I don't know it there is no use...

So let's put my question on different level:

My goal is to rewrite some link to another one

in this case:




without taking any variabiles.So I want to mod_rewrite to take literaly my url and to rewrite it and to redirect it to my desired page.
I've wrote it in this way

RewriteEngine on

RewriteRule ^/testing/index.php?page=about$ /test-page/about [R=301,L]
RewriteRule ^test-page/about$ localhost/testing/index.php?page=about [L]

and there is no change clicking on links

also it could be even more simple example if the url would be:


to rewrite it and redirect it to



 7:25 pm on Jul 26, 2013 (gmt 0)

when I hover over link at testing page,the link doesn't change

My goal is to rewrite some link to another one

Mod_rewrite cannot make or change a URL. Mod_rewrite cannot alter the links on your pages.

You need to physically alter the href value of the links on your page to use the new URL format that you want users to see and use.

Once that new format link is clicked, the browser will make a request for that new format URL.

Once the URL request reaches the server, the URL path will (hopefully) match the RegEx pattern in the rewriting Rule. The internal file pointer in the server will be amended such that Apache will attempt to fetch /index.php?page=about from the hard drive instead of trying to fetch /testing/about-us (which doesn't physically exist as a file of that name) from the hard drive.

The action of altering the internal file pointer so that Apache fetches content from a different internal location to the one specified in the requested URL is called a "rewrite".

This comes as a surprise to many. It's the exact opposite of what many people think a rewrite is, or does.

The links on the site are in the new format and the site works fine with those new URLs, so where's the redirect figure in all this?

Simples! Requesting a URL in the old format will still return content, content identical to that returned when the new format URL is requested. You've run into the classic Duplicate Content problem.

You may think that because the site now links to the new format URLs, Duplicate Content isn't a problem, but it is. Visitors will still be asking for old format URLs after clicking links from other sites and found in stale search engine listings. More importantly, because the old URLs still work and still return "200 OK" status, Google will continue to request those old URLs forever.

This is why you install the redirect. Once that is in place, anyone or anything asking for the old URL is told in no uncertain terms, via the 301 redirect, that the content is at a new URL.

The browser or bot requests that new URL. The new request matches the RegEx pattern in the Rule that rewrites, and Apache proceeds to fetch content from /index.php?page=about as expected.

It is really important that the differences between redirects and rewrites are fully understood. It's massively important to note that a RewriteRule can be configured as a redirect or as a rewrite and you need one of each here.

One final thing to note is that the redirecting Rule must be paired with a RewriteCond testing THE_REQUEST looking for an old format request with parameters. The RewriteCond is VITAL in preventing an infinite redirect-rewrite-redirect-rewrite... loop.

Please also read my step 1, step 2, step 3, post again. It's message #4596309 further up the page.


 8:59 pm on Jul 26, 2013 (gmt 0)

I'm confuse but I will not give it up)))

Since we had disscused about the problem with a string and I decided to capture any variable for result to be:

localhost/var(that was captured.Ok so it works.I get the result in address bar that I've asked

this is the code:

RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)

RewriteRule ^index\.php$ http://localhost/%1? [R=301,L]

But the problem is that I get error 404.

The other part confuses me

Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ longcomplicatedURL?queryname=$1 [L]

This time around, you're capturing part of the request and changing it into a query string.

The puropse of it as I think is to load page using the old ugly url,and the user will not notice it.

Still i tryied to write the code for rewriting.This is how it looks:

RewriteRule ^http://localhost/([a-z]+)$ http://localhost/testing/index.php?page=$1 [L]

Still no result,where is my mistake?

This is really pain in the ass,but I will not quit,before I get it)))
Tnx for patience with me))

[edited by: phranque at 9:51 pm (utc) on Jul 26, 2013]
[edit reason] unlinked urls [/edit]


 9:44 pm on Jul 26, 2013 (gmt 0)

In the RewriteRule that acts as an internal rewrite the RegEx pattern must match the path part of the requested URL. The RegEx pattern cannot ever match the protocol or hostname. The rule target of a rewrite must be the internal filename and parameter where the content really resides on the server hard drive; "internal" means do not include protocol or hostname.

For the redirect, the RegEx pattern in the rule must match the requested path, e.g. index.php, and there must be a preceding RewriteCond looking at THE_REQUEST for the presence of the page=about parameter to ensure that this is an external request and not merely the result of a previous internal rewrite. The rule target will be the full new format URL for the content.

There are two reference systems here. URLs that are used "out there" on the web and server filepaths and filenames that are used "here" inside the server. They are not at all the same thing. They are merely related by the specific server configuration that is applied using various directives including RewriteRules and other things.

If the rule target includes a hostname or the rule has an [R] flag, it is a redirect.

You want to:
1. Link to example.com/about-us from the pages of your site.
2. Redirect requests for example.com/index.php?page=about to example.com/about-us using a RewriteRule configured as a 301 redirect.
3. Rewrite requests for example.com/about-us to internally fetch content from /index.php?page=about on the server hard disk without revealing what that internal location actually is.


 10:29 pm on Jul 26, 2013 (gmt 0)

RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)

RewriteRule ^index\.php$ http://localhost/%1? [R=301,L]
RewriteRule ^localhost/([a-z]+)$ index.php?page=$1 [NC,L]

Ok in first RewriteRule is redirect from page that contains index as the part,and captures the variable page..and on address bar it is shown as like localhost/variable.

So the page doens't open and I get an error,because it could not find the match.

We need one more rule without R=301

To tell server to read localhost/variable as localhost/testing/index.php?page=variable.

At least this is how i got it.

so i made this rewrite rule:

RewriteRule ^localhost/([a-z]+)$ index.php?page=$1 [L]

it says capture variable in () and pass it to index.php?page=$1.

But there is nothing.

Where is my mistake?

Part 2. Rewrite
You get an incoming request for a short pretty URL-- either from a new arrival or from someone who was redirected in Part 1. The server can't tell the difference.

RewriteRule blahblah/([a-z]+)$ longcomplicatedURL?queryname=$1 [L]

This time around, you're capturing part of the request and changing it into a query string.

So I did or tried to do it..but on the page there is no effect,still shows 404error(((

[edited by: phranque at 10:41 pm (utc) on Jul 26, 2013]
[edit reason] unlinked url [/edit]


 1:00 am on Jul 27, 2013 (gmt 0)

Especially becuase I want to learn it good.To know syntax and the use of everything.Becuase if I don't know it there is no use.

Keep saying this periodically. It makes people happy, and happy people are more willing to help :)

Let me try a different way to explain redirect vs. rewrite.

You send your browser to the store. It finds a sign on the door that says "This entrance no longer in use. Go around to the 12th Street side." So the browser has to try a different door. Meanwhile you-- the human-- are waiting in the car. You might not even know that the browser has gone to a different door, unless you happen to look up and see it turning the corner.

Browser comes to the (correct) door and says "I need a gallon of milk. It's in the aisle to the left." Clerk says "You betcha!", ducks into the specified aisle-- and then runs over to a completely different part of the store and comes back with the milk. "Here it is, just where you said."

If you are doing the full redirect-to-rewrite two-step, the milk may actually be located just inside the old door-- the one the browser is no longer allowed to use. But neither you nor the browser knows this.

Now then: The element /localhost/ is not part of the URL path. IGNORE IT, in the same way that you would omit www.example.com on your real site.

In htaccess*, a mod_rewrite pattern begins with the first directory after the domain name. (It may even begin later, if there's no anchor-- or if the htaccess file is located in a deeper directory. But the first directory is the earliest a pattern can ever begin.)

A mod_rewrite target can technically begin in any of three ways:

#1 text without slash

This form is rarely if ever necessary or appropriate, so let's scratch that right away.

#2 http://www.example.com/ where http (or https) is your desired protocol and example.com is any hostname including localhost:some-number-here

When you are testing locally, you could give the full http://localhost:8888 or whatever. But there's no reason to. There's no possibility of giving the wrong form of the hostname; you're obviously not being indexed, so Duplicate Content is not an issue; and https may not even be supported. (I just tried it in MAMP. Browser said nuh-uh, no go. Maybe if I change the default settings, or upgrade to the paid version.) And no matter what you say, you'll have to edit the target for the htaccess of your real site.

What you do need to include in all local testing is the [R=301] flag, because otherwise mod_rewrite won't know that you are redirecting.

That leaves

#3 / (leading slash, meaning start from the root of your current domain-- no matter where the htaccess file is physically located)

When you are testing with localhost, all RewriteRules will have targets in this form. On your live site, it is only used for internal rewrites. That means the rules with [L] flag only.

* Also in <Directory> sections within the config file, but never mind that now. In htaccess you can't make a <Directory> section.


 1:52 am on Jul 27, 2013 (gmt 0)

No seriously,I want to learn it,not because it would make the others happy,but I will be very happy.If I can make others happy that would be a plus.Of course I don't want someone other to do my work,and am
not working anything,that's not fair,and finaly someone will say enough is enough)))But you guys are great,and you are really trying to explain it.I will recomend this forum for sure...

Now back to bussiness...

I would like to make this code right,because with working code I think that I will progress further,and it will be a boost to me.

Ok I made these two rules:

RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)

RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteRule ^([a-z]+)$ index.php?page=$1 [L]

First as I thing is capturing variable from original link and write it new way.
I we tested it without localhost/ and it results with localhost:1337/about and says that connection failed no 404 error.

So ok goal second as I understood is to capture vairable from second redirected link I've tried it with ^([a-z]+)$ and to rewrite it as index.php?page=variable,but it doesn't do do job.I'm I on the wright path with implementing this rewrite rule or not?I'm really confused


 3:05 am on Jul 27, 2013 (gmt 0)

404 means that the requested page doesn't exist. By itself this isn't an awfully useful error message, because it doesn't give any information about rewriting.

The 404 might mean that the file named
doesn't exist-- which you already know. (Except, isn't the URL supposed to be something like /testfiles/about ?)

Or it might mean that some kind of rewrite has taken place, but the file
doesn't exist.

OR, finally, it might mean that everything has happened as intended, but index.php was given a parameter it doesn't know how to handle, so it returned a 404. This is either good or bad, depending on what the parameter was.

In the first two cases, your server logs will show a 404 and the user will see the 404 page. In the third case, server logs will show a 200 but the user's browser will record a 404. The (human) user may or may not see the physical 404 page; it depends on how carefully the index.php page is written. I don't think we are dealing with the third-case scenario here.

It looks as if you have got your blank line in the wrong place. There should be a blank AFTER each RewriteRule, but no blank lines between RewriteCond and RewriteRule. Again, the server does not care about blank lines. (Different from robots.txt, where blank lines do have syntactic meaning.) It is purely for your own benefit. It helps you to see where one ruleset ends and another one begins. (For present purposes, Ruleset = RewriteRule plus accompanying conditions.)

Do your pages now have links in the correct form? <a href = "/about"> or <a href = "/testdir/about"> or whatever you want the final form to be. The redirect is only for insurance against Duplicate Content. It is not an essential part of your rewriting.

Pick some page that really exists on your site. On the local version, not the live site. Let's say you have a real physical file called widgets.html

RewriteRule about /widgets.html [L]

If the RewriteRule works as intended, when you request
the browser's address bar will continue to say "localhost:blahblah/about" but the browser window will show the content of the page

Let me check one other thing to be sure you understand it. In mod_rewrite, any Conditions are listed before the rule they belong to. But even though the conditions are listed first, mod_rewrite will not look at them until it sees that the rule itself might potentially work. So if the rule pattern says
and the server gets a request for blahblah.css, it will not even look at the conditions, and it won't matter if they "fit" or not.

There's another thing you can do on your local WAMP. First shut down the server and make a copy of the config file. Now EDIT the config file-- the real one, not the copy-- to add a RewriteLog:
RewriteLog /logs/rewrite.log
RewriteLogLevel 9

Restart the server and continue testing. But if mod_rewrite already makes you anxious, logging rewrites may not make you any happier.

This 117 message thread spans 4 pages: 117 ( [1] 2 3 4 > >
Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved