Forum Moderators: phranque
http://example.com/index.php?page=about
http://example.com/about
index.php?page=to
http://example.com/$GETvariable name.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1
<li><a href="index.php?page=about">About</a></li>
http://example.com/index.php?page=about
http://example.com/about
[edited by: phranque at 2:24 pm (utc) on Jul 23, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]
I want to rewrite url:
http://example.com/index.php?page=about
to
http://example.com/about
[edited by: phranque at 7:50 am (utc) on Jul 26, 2013]
[edit reason] Please Use Example.com [webmasterworld.com] [/edit]
RewriteEngine On
RewriteRule ^dog.html$ cat.html
This rule that I wrote is redirect,right?
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.
Example:
user asks forwww.example.com/directory/morestuff/index.php?model=volvo
They get bounced over towww.example.com/cars/volvo
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.
Example:
user asks forwww.example.com/cars/volvo
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 fromwww.example.com/directory/morestuff/index.php?model=$1
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.
http://localhost/testing/index.php?page=about
to
localhost/test-page/about
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]
http://localhost/test-page/ on address bar
and 404error
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
When I click on the link,no matter home or about or contact
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]
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]
[^?]+
page=([a-z0-9_-]+)
Well, it's a ### of a lot easier to find imperfections in other people's code than to write perfect code for yourself.
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]
http://localhost:1337/localhost/test-page/about?page=about
RewriteRule ^index\.php$ /localhost/test-page/about$ [R=301,L]
RewriteRule test-page/about$ index.php?page=(about)[L]
RewriteRule test-page/about$ index.php?page=(about) [L]
when I hover over link at testing page,the link doesn't change
it is stil localhost/testing/index.php?page=about
The result on my browser address bar shows error:
http:/ /localhost:1337/localhost/test-page/about?page=about
localhost/testing/index.php?page=about
to
localhost/testing/about-page
RewriteRule ^/testing/index.php?page=about$ /test-page/about [R=301,L]
RewriteRule ^test-page/about$ localhost/testing/index.php?page=about [L]
localhost/testing/about.php
to rewrite it and redirect it to
localhost/testing/about-us
when I hover over link at testing page,the link doesn't change
My goal is to rewrite some link to another one
RewriteEngine on
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)
RewriteRule ^index\.php$ http://localhost/%1? [R=301,L]
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.
RewriteRule ^http://localhost/([a-z]+)$ http://localhost/testing/index.php?page=$1 [L]
[edited by: phranque at 9:51 pm (utc) on Jul 26, 2013]
[edit reason] unlinked urls [/edit]
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.
[edited by: phranque at 10:41 pm (utc) on Jul 26, 2013]
[edit reason] unlinked url [/edit]
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.
RewriteCond %{THE_REQUEST} \?
RewriteCond %{QUERY_STRING} page=([a-z]+)
RewriteRule ^index\.php$ /%1? [R=301,L]
RewriteRule ^([a-z]+)$ index.php?page=$1 [L]
RewriteLog /logs/rewrite.log
RewriteLogLevel 9