Forum Moderators: phranque

Message Too Old, No Replies

Please check my RewriteRule in httaccess

Problem Rewriting URL using htaccess

         

rashidfarooq

10:44 am on Oct 31, 2011 (gmt 0)

10+ Year Member



I want to Rewrite a dynamic URL into a static URL

My URL is such like
http://www.freshupnow.com/movie.php?moviename=A+Flat

I want to rewrite it as below
http://www.freshupnow.com/movies/A-Flat


For this purpose I have written the following Rewrite Rule in .htaccess

Options +FollowSymlinks
RewriteEngine On
#RewriteRule ^movies/([A-Za-z0-9-]+)/?$ movie.php?moviename=$1 [NC,L]

But it does not working, not even generating any error.

Please Note some things before answering.
1. movie.php file exists in root directory
2. .htaccess file exists in root directory
3. I have confirmed from my hosting company that all the services (mod_rewrite etc) are enabled regarding Rewriting URLs.

Please note one thing more
If I write as under

#RewriteRule ^movie\.php$ feedback.php [NC,L]

it redirects to feedback.php page and the URL remains same.
But If I put any character after
^movie\.php$
it doesn't work at all.
I am so worried, I have tried all the possible way, but failed to achieve my target.
Please Help me.

g1smd

11:24 am on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The [NC] flag promotes duplicate content, remove it.

The /?$ promotes duplicate content. Make URLs that do NOT end with a slash the canonical URL. Redirect requests with slash to without slash in a separate preceding rule.

Use the Live HTTP Headers extension for Firefox to investigate the browser requests and the server responses.

Otherwise see [webmasterworld.com...] for a lot of useful comments.

rashidfarooq

11:47 am on Oct 31, 2011 (gmt 0)

10+ Year Member



But it is not even duplicating content. Actually it is not making any effect to the URL.
I have removed the [NC], but still it is not working.

g1smd

11:54 am on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If example.com/Foo and example.com/FOO and example.com/foo all return the same content, then you DO have duplicate content.

rashidfarooq

12:12 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



but content duplication is not my problem. I just want to rewrite the URL.

g1smd

12:48 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



What URL are you requesting? Use example.com for the domain name here in the forum.

What does the Live HTTP Headers extension for Firefox report?

richtalbot

12:53 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



'L' normally means 'Last rule', so it should only be on your last rule.

g1smd

1:14 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The [L] flag means "halt further mod_rewrite processing if this rule matched".

The [L] flag should be on every rule.

The [L] flag makes this matching rule the last processed rule for the current request.

rashidfarooq

1:26 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



@g1smd

I am requesting the following URL
http://www.example.com/movie.php?moviename=A+Flat

I want to rewrite it as below
http://www.example.com/movies/A-Flat

Please review my first post in this thread. and you will understand my point.

g1smd

1:43 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Mod_rewrite cannot change your URL. URLs are defined in links.

You need to link to http://www.example.com/movies/A-Flat from the pages of your site. Once the link is clicked, you'll request http://www.example.com/movies/A-Flat

Once that request reaches the server, mod_rewrite will then rewrite the request and fetch content from /movie.php?moviename=A+Flat from within the server. The rule target is an internal filepath and the rule needs only the [L] flag.

If search engines have already indexed example.com/movie.php?moviename=A+Flat then you need an additional ruleset to redirect those requests to your new URL. This ruleset will use a RewriteCond to test %{THE_REQUEST} to ensure that requesting parameters is not as a result of a previous rewrite; that this really is an external request. The rule target will be a URL, complete with protocol and domain name and the rule will use the [R=301,L] flags.

The other thread explains more about this, and there are hundreds of prior code examples because this question is asked several times each week.

rashidfarooq

2:43 pm on Oct 31, 2011 (gmt 0)

10+ Year Member



@g1smd
you are right. There are links in my site. such like
http://www.example.com/movie.php?moviename=A+Flat
when user clicks this link, this link will be put up to browser address bar and then I want to rewrite this URL to
http://www.example.com/movies/A-Flat
but with all of my efforts, I am unable to do that.
Please give me some precise solution. I'll be thankful to you.
I am ready to pay some money if some one solve this problem. It has become a great headache for me. I am trying to solve this problem from last 5 days.
Please review my first post in this thread to get more explanation.

g1smd

4:10 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I fully understand the problem as I have answered the same question twice per week for the last five years in this forum.

From the thread that I linked to and which you refuse to read.

"URLs are defined in links. It is too late to change the URL once a link is clicked. If you want to change a URL, you must change the links on your pages."

If someone requests the wrong URL, you can redirect that request to a different URL. Both URLs will exist as long as there is a link pointing to the incorrect type of URL.

For your exact problem, you cannot rewrite this request using a RewriteRule. You must redirect it using a RewriteRule.

You'll still need your rewrite to fetch the content once the correct URL is being requested. But you need to add a redirect to redirect incorrect requests to the correct URL. Both the rewrite and the redirect will use a RewriteRule.

lucy24

4:21 pm on Oct 31, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Let's backtrack.
If I write as under
RewriteRule ^movie\.php$ feedback.php [NC,L]

it redirects to feedback.php page and the URL remains same.


No. It doesn't. It REWRITES to feedback.php page. It is absolutely crucial that you get a firm grip on the difference.

But If I put any character after ^movie\.php$ it doesn't work at all.


Do you mean "after" in your request or "after" in your rule? The pattern in your rule is anchored at both ends, so it will only match for the exact form "movie\.php". This does not count the query string, which is ignored in the "pattern" part of a RewriteRule. To work with the existing query, you have to add a Condition. (Forums search will give you detailed information.)

You may have misunderstood the reference to Duplicate Content. It does not mean that your site is acquiring new pages that are all the same. It means that search engines think you have lots of pages that are all the same, because PageName.html is not the same thing as pagename.html or Pagename.html and so on. In general you should only use [NC] with redirects. (Users, including search engines, know when they have been redirected. But a rewrite is invisible.)

rashidfarooq

11:04 am on Nov 1, 2011 (gmt 0)

10+ Year Member



Thanks to all for answering Me. I have understood the problem. The problem was in the links in my page. I changed the links according to the direction of g1smd and now every thing is working.

g1smd

12:12 am on Nov 2, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The next step is to add the redirect so that if the old URLs are requested by someone following out of date links from other sites or clicking a stored link in their bookmarks, the user is redirected to the new URL. The code for this is in many previous threads. It will include a RewriteCond looking at %{THE_REQUEST} as well as the RewriteRule with [R=301,L] flags.