Forum Moderators: phranque

Message Too Old, No Replies

Rewrite URL with no .php

Rewrite URL with no .php

         

huuya

11:05 pm on Jan 1, 2011 (gmt 0)

10+ Year Member



I was wondering if anyone can help me?
How can I change this:

http://www.example.com/huuya.php?page=huuya-review-page1

into

http://www.example.com/huuya-review-page1/

or

http://www.example.com/huuya-review-page1.html


using the .htaccess mod rewrite?
any help appreciated.

[edited by: jdMorgan at 7:42 pm (utc) on Jan 5, 2011]
[edit reason] used example.com to prevent exploits until code is fixed [/edit]

wilderness

11:22 pm on Jan 1, 2011 (gmt 0)

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



rewrite+php+html [google.com]

redirect+php+html [google.com]

mvpetrovich

9:40 pm on Jan 3, 2011 (gmt 0)

10+ Year Member



RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^(.*)/images/(.*)$
RewriteCond %{REQUEST_FILENAME} !^(.*)/css/(.*)$
RewriteCond %{REQUEST_FILENAME} !^(.*)/js/(.*)$
RewriteCond %{REQUEST_FILENAME} !^(.+)/huuya\.php$
RewriteRule ^(.*)\.* huuya.php?page=$1

g1smd

11:27 pm on Jan 3, 2011 (gmt 0)

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



The multiple (.*) patterns will slow your server to a crawl, needing many thousands of back off and retry attempts per page request.

They must be changed for something more specific and which can be read in a single left-to-right pass.

\.* is a pattern that patches multiple periods, like "something........". . Is that what you wanted?

You must add the [L] flag to every rule.

mvpetrovich

2:15 am on Jan 4, 2011 (gmt 0)

10+ Year Member



The multiple (.*) patterns will slow your server to a crawl, needing many thousands of back off and retry attempts per page request.

They must be changed for something more specific and which can be read in a single left-to-right pass.

\.* is a pattern that patches multiple periods, like "something........". . Is that what you wanted?

You must add the [L] flag to every rule.


The (.*) is a Reg expression group. The parenthesis are not needed in the conditions. It is needed in the RewriteRule.

The RewriteRule DOES NOT need the "\.*" That was something left over from another application.

[L] will tell Apache to not to process any more rules if the one is used. It should be on the rule not the conditions.

Here are the corrections:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !^.*/images/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/css/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/js/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/huuya\.php$
RewriteRule ^(.*) huuya.php?page=$1 [L]


I have used this code for so many years, I had not looked at it critically in awhile. Glad I posted it for your feedback. Thanks!

huuya

4:27 am on Jan 4, 2011 (gmt 0)

10+ Year Member



mvpetrovich wrote:

>> RewriteEngine on
>> RewriteCond %{REQUEST_FILENAME} !^.*/images/.*$
>> RewriteCond %{REQUEST_FILENAME} !^.*/css/.*$
>> RewriteCond %{REQUEST_FILENAME} !^.*/js/.*$
>> RewriteCond %{REQUEST_FILENAME} !^.*/huuya\.php$
>> RewriteRule ^(.*) huuya.php?page=$1 [L]

still. got no luck. even for the simple:

:-(

[edited by: jdMorgan at 7:43 pm (utc) on Jan 5, 2011]
[edit reason] Removed URL to prevent competitor exploits. [/edit]

mvpetrovich

7:01 pm on Jan 4, 2011 (gmt 0)

10+ Year Member



Some hosting environments require the RewriteBase command.

RewriteEngine on
RewriteBase /
...


The examples I was giving you, using the RewriteCond were to prevent redirection of folders where you might have CSS, Javascript, and images. You may have to change these or add others.

mvpetrovich

7:06 pm on Jan 4, 2011 (gmt 0)

10+ Year Member



PS. I like your site. It is very clean in appearance and nicely laid out. Also, check your image tag sizes. You have a few that have height and width with "px" appended, and that should not be there.

jdMorgan

7:41 pm on Jan 5, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, you will have to change all the links on your pages to the new format. The links on your pages define the URLs, and mod_rewrite cannot be used to re-define URLs.

Once you've done that, this rule will rewrite client requests for the new-format URLs to the proper script-filepath-plus-query-string needed to generate and serve the proper content:

RewriteRule ^example-review-page([0-9]+)$ /example.php?page=huuya-review-page$1 [L]

If you don't already have other working rewrite rules, then you will need to set up and enable mod_rewrite. Please let us know if this is the case.

Jim

[edit] Corrected as noted below. [/edit]

[edited by: jdMorgan at 10:15 pm (utc) on Jan 6, 2011]

huuya

9:43 pm on Jan 5, 2011 (gmt 0)

10+ Year Member



HUUYAAA..., YEAH..., THAT WORKS! ALRIGHT! :-)
thanks so much jdMorgan, mvpetrovich, (and others).
So to close this topic:

To change this URL:

http://www.huuya.com/huuya.php?page=huuya-reviews


into

http://www.huuya.com/huuya-reviews


this is what i need to put in ".htaccess":

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !^.*/images/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/css/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/js/.*$
RewriteCond %{REQUEST_FILENAME} !^.*/huuya\.php$
RewriteRule ^(.*) huuya.php?page=$1 [L]


and this is what i need to do next:

- change all links into url without the "huuya.php?page="
- example: in my homepage, menu option: "Reviews"

previously >>> http://www.huuya.com/huuya.php?page=huuya-reviews
now >>> http://www.huuya.com/huuya-reviews


this hopefully can help others who have same case as mine.

Credits go to jdMorgan and mvpetrovich and other great
people at [webmasterworld.com....]

huuyaaaaaa... [huuya.com...]

g1smd

10:12 pm on Jan 5, 2011 (gmt 0)

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



@jd There's maybe a typo in #4249497 the target for the _rewrite_ shows a domain name.

Tried PM: "Mailbox full".

wilderness

10:30 pm on Jan 5, 2011 (gmt 0)

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



this hopefully can help others who have same case as mine.


Please pardon my intrusion, however, out of respect for Jim's (jdmorgan) efforts and as a gratitude of thanks?

You might read the forum charter for submissions and regarding the exclusion of domain names (rather "example.com") and active links.

This compliance of the charter would save jim or another administrator the time required to edit you active domain and/or link, replacing with example.com

huuya

1:13 am on Jan 6, 2011 (gmt 0)

10+ Year Member



my big apology to everyone here in this forum
if my posts inappropriate.

huuya

1:20 am on Jan 6, 2011 (gmt 0)

10+ Year Member



oh, to mvpetrovich about my site layout:

- thank you. :-) it's a project of me & my students.
i may ask you and others about the design in right forum
once it's completed.

- "px" gone. thanks. :-)

jdMorgan

10:20 pm on Jan 6, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Efficiency tweak for .htaccess code:

RewriteCond $1 !^huuya\.php$|images/|\.(css|js)$
RewriteRule ^(.*) huuya.php?page=$1 [L]

If your huuya.php script *does not* generate or handle requests for images, then excluding all image filetypes using the first RewriteCond would likely speed up your site quite a bit.

Jim

[edited by: jdMorgan at 4:15 pm (utc) on Jan 9, 2011]

mvpetrovich

10:25 pm on Jan 7, 2011 (gmt 0)

10+ Year Member



@jdMorgan, Excellent tweak.

I had not used $1 in a condition, and it appears to only allow one RewriteCondition with the $1 test string. (I have a case where I need more than one, but grouping with a Reg Expression OR is much better approach.)

Also %{REQUEST_URI} could have been used instead of %{REQUEST_FILENAME}, and "!^.*/" could have then just been "!^/"

jdMorgan

4:22 pm on Jan 9, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I had not used $1 in a condition, and it appears to only allow one RewriteCondition with the $1 test string.

RewriteCond $1 !^images/
RewriteCond $1 !^admin/

Would be perfectly-valid where a negative match is desired, while

RewriteCond $1 ^something [OR]
RewriteCond $1 ^something-else

would work fine where a positive match is needed.

> ... but grouping with a Reg Expression OR is much better approach.

True in .htaccess, false in server config files, where the code is "compiled" at server start-up. In that case, separate RewriteConds are actually processed faster.

> ... and "!^.*/" could have then just been "!^/"

That should actually be "!/" instead of "!^.*/". But it would be better to specify the entire correct local URL-path prefix in these patterns instead of using either of these ambiguous matches.

Jim