homepage Welcome to WebmasterWorld Guest from 54.161.247.22
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

    
Shortening index.php?var= in the URL
php htaccess? shorten url veriable
hottrout




msg:4383492
 1:22 pm on Nov 4, 2011 (gmt 0)

Hi again,

I am working on trying to simplify my PHP URLs with a view to making them more Google and user friendly. I have managed to remove the need for index.php in the url by using the following code.

RewriteRule ^index\.php$ http://www.example.com/emulators/%1 [R=302,L]

The normal URL looks like this

http://www.example.com/emulators/index.php?folder=Atari

with this code it now looks like this

http://www.example.com/emulators/?folder=Atari

What I want to know is. Is this as far as I can go in terms of shortening the url or is there a way to also remove the ?folder= from the url.

I know that RewriteRule does not handle php variables and can not figure out a way to do this. It is important to note that I do not want to redirect but simply want to mask/hide some of the parameters being passed on. This in turn should make my google URLs look a lot more simple and also hopefully static.

All help and suggestions are appreciated.

EDIT : I t is also important to note that I use the following code in my root htaccess file to prevent index.php appearing at directory urls.

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

Maybe I should be modifing this rewrite instead?

 

g1smd




msg:4383524
 2:59 pm on Nov 4, 2011 (gmt 0)

This question has been asked several times this week, and many times this year.

URLs are used out on the web, so you can make them have whatever format you want.

Start by reading at least the second post in this thread: [webmasterworld.com...]

hottrout




msg:4383527
 3:10 pm on Nov 4, 2011 (gmt 0)

Thank you, that confirms what I expected. Knowing this, Can I ask why this code

RewriteRule ^index\.php$ http://www.example.com/emulators/%1 [R=302,L]

sucessfully removes the index.php part of the url but the url remains working? Is this down to specific allowances for index.php as a php name?

g1smd




msg:4383531
 3:26 pm on Nov 4, 2011 (gmt 0)

The %1 will be blank/empty and a request for a folder (URL ending in slash) will map to the index.php file through the DirectoryIndex directive.

hottrout




msg:4383537
 3:40 pm on Nov 4, 2011 (gmt 0)

I understand. Thanks for clearing that up. Is it good practice or does it provide a google url benefit to remove the index.php from the url?

Also can I modify this command in the root htaccess to do the job of both?

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ ([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

g1smd




msg:4383569
 4:47 pm on Nov 4, 2011 (gmt 0)

Yes, do remove index filenames from URLs.

Do redirect those requests to URL ending in slash.

Make sure this rule is always BEFORE your more general non-www to www redirect.

hottrout




msg:4383570
 4:56 pm on Nov 4, 2011 (gmt 0)

OK thanks for that. At present my htaccess redirects any request for index.php to the directory with a trailing slash as you have suggested. So,

www.example.com/index.php goes to www.example.com/

and

www.example.com/emulators/index.php goes to www.example.com/emulators/

however if I choose a link that uses a php variable like this

www.example.com/emulators/index.php?folder=Atari

it does not strip out the index.php. Can you suggest why?

g1smd




msg:4383583
 5:17 pm on Nov 4, 2011 (gmt 0)

It ignores the request because your RewriteCond pattern matches index.php only without parameters attached in the URL request.

I would NOT strip out parameters. If you do anything, do this:

- Link to the URL
www.example.com/emulators/Atari from the pages of your site and set up an internal rewrite to fetch content from the path /emulators/index.php?folder=Atari

- Set up a redirect that triggers only on external URL requests for
www.example.com/emulators/index.php?folder=Atari and redirects to www.example.com/emulators/Atari.
hottrout




msg:4383589
 5:25 pm on Nov 4, 2011 (gmt 0)

OK, I understand. Would it also be bad form to simply remove the index.php part from the url and leave them looking like this

www.example.com/emulators/?folder=Atari

If this is also good to do, how to I change my code to make it achieve this?

g1smd




msg:4383590
 5:35 pm on Nov 4, 2011 (gmt 0)

I see no point in removing index.php and still working with parameters.

If you're going to remove index.php just go completely extensioness.

hottrout




msg:4413126
 4:36 pm on Feb 1, 2012 (gmt 0)

Would there be a benefit in removing the index.php to shorten the url indexed by google and therefore help your SEO for that URL?

The reason I ask is because there is a lot of rewriting to be done if I try to go completely extensioness.

g1smd




msg:4413239
 9:22 pm on Feb 1, 2012 (gmt 0)

By going extensionless you can much simplify the URL structure for your site.

There's long term benefits in doing this; future URL maintenance may be much reduced.

hottrout




msg:4413290
 11:25 pm on Feb 1, 2012 (gmt 0)

The problem i have is that the URL paths to the different sections is automatically created by the php code. In other words, all I have to do is add a folder to the site and the php script picks up the new folder and creates a link to it. It does this by using the parameter ?folder=

Should I re-write the script to create the URL to the direct folder each time?

hottrout




msg:4413291
 11:30 pm on Feb 1, 2012 (gmt 0)

# Send index.php to root url
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

This is the code I use to remove index.php from base pages but it does not remove index.php when the ? follows. How can I amend this code to also remove index.php? from the url

This is an example url:-

http://www.example.com/files/index.php?folder=Casio

I would prefer if it was shortned to

http://www.example.com/files/?folder=Casio

This would be a start and I could get working on re-writing the php code.

g1smd




msg:4413292
 11:41 pm on Feb 1, 2012 (gmt 0)

THE_REQUEST contains the literal GET request sent by your browser.

The RegEx pattern in the RewriteCond will need to be adjusted to match exactly what the browser actually sends after the "GET" part and before the "HTTP/1.1" part.

hottrout




msg:4413313
 12:16 am on Feb 2, 2012 (gmt 0)

I am sorry but I am only a novice at this

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\?\ HTTP/

hottrout




msg:4413315
 12:23 am on Feb 2, 2012 (gmt 0)

Is this more what you meant

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php([^/]+/)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php(([^/]+/)*)$ http://www.example.com/$1$2 [R=301,L]

lucy24




msg:4413331
 12:43 am on Feb 2, 2012 (gmt 0)

Should I re-write the script to create the URL to the direct folder each time?

Generic answer: any links within your site must point to the "correct" URL-- that is, the one you want users and search engines to see. So if links are generated by a php script, make sure the script generates links in their displayed form, not their behind-the-scenes form.

Oops, overlapping:
RewriteRule ^(([^/]+/)*)index\.php(([^/]+/)*)$ http://www.example.com/$1$2 [R=301,L]


The RewriteRule itself doesn't "see" the host or the query. So the second capture would always be empty. If $2 is meant for the query string, it can only come from THE_REQUEST or from QUERY_STRING. So it would be %2 (or %1 or whatever number it comes out to be) for material captured in a Condition.

hottrout




msg:4413596
 5:53 pm on Feb 2, 2012 (gmt 0)

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php([^/]+/)\ HTTP/
RewriteRule ^index\.php(([^/]+/)*)$ http://www.example.com/$1%2 [R=301,L]

Does this work?

lucy24




msg:4413635
 7:54 pm on Feb 2, 2012 (gmt 0)

If the rule is only meant for requests with a query string ending in / and containing no other / then it will work. Scrolling back to your original post suggests that this may really be what you're doing.

hottrout




msg:4422658
 2:20 pm on Feb 28, 2012 (gmt 0)

After reading a lot more I spent some time thinking through everything that you guys had said. I have started working on my PHP scripts and recoding them so that they produce a more friendly url internally like this :-

www.example.com/emulators/atari/800

the actual code that the script requires look like this

www.example.com/emulators/index.php?folder=atari/800

I understand that I need to create an entry in the htaccess that will capture these pretty URLs and deliver them as the actual script requires but I am unsure as to what that code should be. Could you give me an example of the code, note that the number of urls after the emualtor/ could be as many as 6 e.g. www.example.com/emulators/atari/800/xl/tapes/bin/etc/


Also you explained that all the existing external links that are coming in expecting www.example.com/emulators/atari/800/xl/tapes/bin/etc/ need to be captured and then delivered to the pretty URLs as well.

How exactly to I achieve both from within the same htaccess ? Or how to I capture the URL requests when they are from ourside the site.

g1smd




msg:4422781
 7:16 pm on Feb 28, 2012 (gmt 0)

RewriteRule responds to any URL request and unless it examines the HTTP_REFERER variable has no idea where that click came from.

The following rule matches the pretty URL request and internally rewrites the pointer to point at the actual internal filepath inside the server filesystem that will deliver the content:

RewriteRule ^emulators/atari/800$ /emulators/index.php?folder=atari/800 [L]

In real life the rule pattern would contain a regular expression to match URLs all having the same pattern and the rule target would contain backreferences re-using those values.

RewriteRule ^emulators/(([^/]+/)*[^/.]+)$ /emulators/index.php?folder=$1 [L]

Your requested page URL must NOT end with a trailing slash (be aware that in your examples one did and one did not - attention to this small detail is the crucial difference between success and failure).

hottrout




msg:4422792
 7:31 pm on Feb 28, 2012 (gmt 0)

You are correct I made a typo. The actual requested page URL nevers ends with a slash so your code should work. Does this rewriterule cope with many folders deep? Could you please help an old guy liek me and explain (([^/]+/)*[^/.]+) so that I fully understand it.

If this then copes with all links both from inside the site and from outside the site. What code do I need and where do I place it to make sure that the 100's of existing outside links that are expecting to go to /emulators/index.php?folder=atari/800 will also still work, without getting caught in a loop?

g1smd




msg:4422798
 7:51 pm on Feb 28, 2012 (gmt 0)

(([^/]+/)*[^/.]+) is "not a slash (one or more times) followed by a slash" [i.e. a folder], the whole "zero or more times" [i.e. any level of folders including none] followed by "not a slash or period one or more times" [i.e. a file without an extension].

So, that's the internal rewrite sorted.

You now need an external redirect to redirect from URLs with parameters to the friendly URL. To prevent a redirect-rewrite loop this redirect also needs a preceding RewriteCond that looks at THE_REQUEST. This will ensure that when the pointer contains parameters, the redirect is invoked only if those parameters were within the original external URL request and not as a result of a prior rewrite.

There's a few hundred previous threads with some example code to get you started. This is a topic that comes up almost every week, sometimes several times per week. :)

lucy24




msg:4422887
 11:33 pm on Feb 28, 2012 (gmt 0)

I don't know what I was doing the last time I posted to this thread, but clearly it didn't leave me any brain cells to spare, because what I said was perfect gibberish. (Compare second-to-last post, which was perfectly rational.) Oops. Pay no attention to that post behind the curtain.

g1smd




msg:4422929
 1:29 am on Feb 29, 2012 (gmt 0)

My brain is fried like that sometimes. I mean, I have been editing a htaccess file, editing a massive spreadsheet in G docs, and talking on skype while also posting in several WebmasterWorld threads and reading stuff on two other forums.

hottrout




msg:4423021
 10:02 am on Feb 29, 2012 (gmt 0)

LOL, my head is like that most of the time and that is onyl as a direct result of having young children in the house.

That Lucy is what we call a bum steer.

g1smd, could you post a link to the most useful thread that I can use please. Thank you by the way.

g1smd




msg:4423228
 7:53 pm on Feb 29, 2012 (gmt 0)

With respect, I don't have the time to make a search. It's 8 p.m. here and I have a report to write, two phone calls I have to make, five emails to reply to and someone I must catch on Skype before they go to bed. I should also delve into WMT and analytics for two sites but will have to put that off.

hottrout




msg:4423240
 8:17 pm on Feb 29, 2012 (gmt 0)

Understood and thank you already.

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