homepage Welcome to WebmasterWorld Guest from 54.237.98.229
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 47 message thread spans 2 pages: < < 47 ( 1 [2]     
Hide/changing .php?=
HaloPlayer



 
Msg#: 4501915 posted 6:14 am on Sep 30, 2012 (gmt 0)

Hi,
First time poster here, I came across this forum after searching for htaccess mods.

My question is about hiding the php?= in a URL, I tried a forum search and found this but it's not quite what I am after: [webmasterworld.com...]

My question is this, if a URL looks like this:
www.example.com/book.php?name=motor_car
can I use a htaccess rewrite to make it look more clean like
www.example.com/book/name/motor_car

Also does doing this create extra server load?

Thanks in advance.

[edited by: incrediBILL at 9:34 pm (utc) on Oct 7, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

 

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4501915 posted 11:09 am on Oct 24, 2012 (gmt 0)

i would recommend the 2nd example but you probably want to use a 301 redirect.

then post your entire redirect/rewrite cycle for review.
when you do so use example.com instead of mydomain.site.

HaloPlayer



 
Msg#: 4501915 posted 10:11 am on Oct 25, 2012 (gmt 0)

Sorry about that phranque, I copied and pasted it without thinking to edit it.

Current rewrite:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/([-a-zA-Z0-9_]+) /book.php?name=$1 [L]


This is the 2nd redirect with the example.com:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^page\.php$ http://example.com/page/%1.pdf [R=302,L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 10:22 am on Oct 25, 2012 (gmt 0)

You need
RewriteEngine On only once.

Since your page names are extensionless, there is no need for the
RewriteCond %{REQUEST_FILENAME} !-f
test. You'll never have an extensionless file stored on the server.

([0-9]*) allows a blank ID. Replace * with + here.

([0-9]+)$ - The $ does not allow for redirecting when there are additional parameters. Use ([0-9]+)(&|$) here.

The 302 should be a 301 redirect.

HaloPlayer



 
Msg#: 4501915 posted 2:05 pm on Oct 26, 2012 (gmt 0)

Thanks g1smd.

How should I structure this though, this is what is confusing me:

RewriteRule ^book\.php$ http://example.com/book/%1.pdf [R=301,L]

The original rule which I have working (obviously without a redirect) is:

RewriteRule ^book/([-a-zA-Z0-9_]+) /book.php?name=$1 [L]

I'm having difficulties putting the two together, I have tried a few different variations with no success.

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 5:04 pm on Oct 26, 2012 (gmt 0)

The code in post 4512182 with the corrections in post 4512197 should get you close, although the rules you showed don't seem particularly "mirrored".

They are the rules for the same piece of content?

HaloPlayer



 
Msg#: 4501915 posted 1:03 pm on Oct 27, 2012 (gmt 0)

Yes sorry about that, they are mean't to be for the same piece of content, I had copied and pasted the example without changing it for my usage.

So an update on this, I tried using many different variations of the code and did not have any luck, my URL output kept redirecting with each time I changed some code for example:

http://example.com/book/motor_car.php?name=motor_car

http://example.com/book/?name=motor_car
Or it would loop.

After doing a search I did find an article which explains that adding a ? at the end which made the correct URL but it looped.

The code I am currently using which loops is:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/([-a-zA-Z0-9_]+) /book.php?name=$1 [L]
RewriteCond %{QUERY_STRING} ^name=([-a-zA-Z0-9_]*)(&|$)
RewriteRule ^book\.php$ http://example.com/book/%1? [R=301,L]


So I think I am in the right direction.

The original
http://example.com/book/%1.pdf [R=301,L] will obviously redirect to a .pdf URL but I am chasing a extensionless URL this is why I removed the .pdf and added the ?
HaloPlayer



 
Msg#: 4501915 posted 1:36 pm on Oct 27, 2012 (gmt 0)

Also, one other thing I completely over-looked, having a sitemap for the Google spider will these rewrite/redirect changes have any effect on this?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 1:47 pm on Oct 27, 2012 (gmt 0)

A few points to get you on the right track.

Add a blank line after every RewriteRule so you can see each ruleset more clearly.

Comment the code indicating which rules redirect and which rules rewrite.

You must list the external redirects before the internal rewrites. The final external redirect is usually the non-www/www canonical redirect, listed immediately before the first internal rewrite.

As I said before, you do not need the line
RewriteCond %{REQUEST_FILENAME} !-f
because you will not have extensionless files on the server harddrive.

^name=([-a-zA-Z0-9_]*)(&|$) - the * allows a blank name. Use a + here.


If you are redirecting requests with name= parameter in the requested URL to friendly URLs and then rewriting requests for friendly URLs to an internal filepath and passing a name= parameter to it, the rules will loop. This happens because the internal rewritten request will rematch the rule that produces the redirect. This will expose the rewritten internal filepath back out on the web as a new URL and this request will be redirected again. To stop this happening, the rule that redirects MUST also test THE_REQUEST in a preceding RewriteCond to ensure that only incoming external URL requests for the name= parameter are redirected and not those as a result of a previous internal rewrite.

HaloPlayer



 
Msg#: 4501915 posted 11:42 am on Nov 3, 2012 (gmt 0)

I finally got it to work! Thanks for the tips and patience g1smd, I really do appreciate it :-)

The final code, so other people can use if if they find they are in a similar situation to me, if you wouldn't mind checking it out, will this cause any unwanted side effects?:

Options +FollowSymlinks
RewriteEngine on
RewriteCond %{THE_REQUEST} name=([-a-z0-9_]+)
RewriteRule ^book\.php$ /book/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/([-a-z0-9_]+)(&|$) /book.php?name=$1 [L]


So in the end I replaced the
%{QUERY_STRING} with %{THE_REQUEST}

I'm a little confused whether this is the appropriate fix though, becase a few of the examples used this as well:

%{THE_REQUEST} ^[A-Z]{3,9}

The {3,9} must have some significance because it is repeated in the examples.
And since I have replaced the
%{QUERY_STRING}
with
%{THE_REQUEST} haven't I made the query string condition redundnant?, yet it still works?

As silly as it sounds I see a lot of sites who use extensionless URL's still allow for a trailing / at the end of a URL as well as a URL without one, e.g. http://www.example.com/mypage also works: http://www.example.com/mypage/

The above code does not allow this, is it worth tweaking it to allow this?

Sorry for all the questions, I got a bit excited :-)

HaloPlayer



 
Msg#: 4501915 posted 12:31 pm on Nov 3, 2012 (gmt 0)

Also for anyone that needs to change a mass of links image sources or file names or even html tags with a different name or tag, like I have to do this tool is the king, its a Batch Replacer: [replsoft.com...]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 12:52 pm on Nov 3, 2012 (gmt 0)

If you allow trailing slash and not-trailing slash to both directly return content with 200 OK status, then you have created a duplicate content problem.

For folders, URL with slash should return content and URL without slash (and URL with index filename) should redirect.

For pages, URL without slash should return content and URL with slash should either redirect or return 404 Not Found.


%{THE_REQUEST} ^[A-Z]{3,9} - this part matches the literal GET or POST method found in the HTTP request header sent by the browser:
GET /book.php?name=this_one_here HTTP/1.1

QUERY_STRING looks at the query string part only, and this may have already been modified by a previous internal rewrite. You look at THE_REQUEST to ensure that the query string was part of the original request that the browser made and not created as a result of a previous internal rewrite. Looking at THE_REQUEST in this way prevents an infinite redirect->rewrite loop.



The redirect target should contain the canonical protocol and hostname.

Leave a blank line after every RewriteRule to make the code easier to read.

Directly after your "specific" redirect, you need to add the "general" non-www/www canonicalisation redirect.

The end anchoring in the pattern in the Rule which rewrites should be $ not (&|$)

HaloPlayer



 
Msg#: 4501915 posted 6:08 am on Nov 5, 2012 (gmt 0)

Thanks!

I've tidied it up a bit:

Options +FollowSymlinks

RewriteEngine on

RewriteCond %{THE_REQUEST} name=([-a-z0-9_]+)

RewriteRule ^book\.php$ /book/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d

RewriteRule ^book/([-a-z0-9_]+)$ /book.php?name=$1 [L]



You say the redirect target should contain the cannonical protocol and hostname, are you referring to this?:
HTTP/1.1 Is this just placed at the end of the redirect target?

And when you say "Directly after your "specific" redirect, you need to add the "general" non-www/www canonicalisation redirect."
You are just referring to the base URL of the site?

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 10:56 am on Nov 5, 2012 (gmt 0)

The redirect target should contain the canonical protocol and hostname.
In the rule that redirects, the
/book/%1? part should be http://www.example.com/book/%1?


Leave a blank line after every RewriteRule to make the code easier to read.
That's after each Rule, not after each Condition.


Directly after your "specific" redirect, and before the internal rewrite, you need to add the "general" non-www/www canonicalisation redirect.
After the redirecting ruleset and before the rewriting ruleset add:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

HaloPlayer



 
Msg#: 4501915 posted 7:38 am on Nov 6, 2012 (gmt 0)

Thanks I understand now, you are referring to the http:// & www parts of the domain name, so instead of the URL being accessed in two ways e.g. http://www.example.com or http://example.com the rewrite makes a permanenet redirect to the www e.g. http://example.com redirects to http://www.example.com

The code:

Options +FollowSymlinks
RewriteEngine on

RewriteCond %{THE_REQUEST} name=([-a-z0-9_]+)
RewriteRule ^book\.php$ http://www.example.com/book/%1? [R=301,L]

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^book/([-a-z0-9_]+)$ /book.php?name=$1 [L]

HaloPlayer



 
Msg#: 4501915 posted 8:08 am on Nov 11, 2012 (gmt 0)

Thank you again g1smd for your time :-) I wish there was a way of giving you rep points or something but this forum does not have this option.

Just to finalise, that above code should not pose any security threats or cause any unwanted problems? It seems to be working fine, would be good to know it is correct though.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4501915 posted 12:36 pm on Nov 11, 2012 (gmt 0)

it seems like you have this out of order:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
RewriteRule ^book/([-a-z0-9_]+)$ /book.php?name=$1 [L]


shouldn't it look like this?:

RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^book/([-a-z0-9_]+)$ /book.php?name=$1 [L]

g1smd

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4501915 posted 3:08 pm on Nov 11, 2012 (gmt 0)

It should.

Looks like a cut and paste error.

This 47 message thread spans 2 pages: < < 47 ( 1 [2]
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