Jump to content

Recommended Posts

Hello.  I need help with a mod_rewrite that will take this URL...

www.mydomain.com/client1/menu

and load this URL...

www.mydomain.com/client1/menu.php

 

I tried this but it isn't working...

	RewriteCond %{REQUEST_URI} ^.*/menu\.php
	RewriteRule ^(.*)menu.php$ $1 [L,R=301]
	

 

Thanks.

 

Link to comment
https://forums.phpfreaks.com/topic/309617-removing-php-not-working/
Share on other sites

You've written a rule that will turn /menu.php into /menu.php.

People think of this stuff backwards. You are not removing the .php extension. You are adding it. As in you want someone to see /menu in their browser and have the server add the .php extension.

18 minutes ago, requinix said:

You've written a rule that will turn /menu.php into /menu.php.

How so?

 

	RewriteCond %{REQUEST_URI} ^.*/menu\.php
	RewriteRule ^(.*)menu.php$ $1 [L,R=301]
	

 

In the second line, (.*) would select everything after the hostname (e.g. "www.mysite.com") and before menu.pho

And $1 would then contain that value above, minus the "menu.php"

 

18 minutes ago, requinix said:

People think of this stuff backwards. You are not removing the .php extension. You are adding it. As in you want someone to see /menu in their browser and have the server add the .php extension.

 

Okay, you got me there!

 

13 minutes ago, SaranacLake said:

How so?

Oh, yeah, sorry I kinda glossed over it: you're removing /menu.php entirely. Presumably you have menu.php as an index, or have set up another rewrite so that /whatever/ is mapped to /whatever/menu.php.

1 minute ago, requinix said:

Oh, yeah, sorry I kinda glossed over it: you're removing /menu.php entirely.

No, teacher, I am *adding* "menu.php" based on what you said before!  😉

 

1 minute ago, requinix said:

Presumably you have menu.php as an index, or have set up another rewrite so that /whatever/ is mapped to /whatever/menu.php.

Yes, in the "client1" directory, "menu.php" is like an index file.  (I was going to use "index.php" a second time, but thought that might be confusing, so I opted for "menu.php".)

 

So I want the "pretty" URL www.mydomain.com/client1/menu to map to the real URL www.mydomain.com/client1/menu.php

 

 

2 minutes ago, SaranacLake said:

No, teacher, I am *adding* "menu.php" based on what you said before!  😉

Not this time 😁 This specific rule really is about removing the "menu.php". The other rule (which you haven't shown) is about adding it.

2 minutes ago, SaranacLake said:

(I was going to use "index.php" a second time, but thought that might be confusing, so I opted for "menu.php".)

Doesn't matter if you're hiding it, however...

2 minutes ago, SaranacLake said:

So I want the "pretty" URL www.mydomain.com/client1/menu to map to the real URL www.mydomain.com/client1/menu.php

Right. You need something to add the extension back in. Apparently instead of the rule you posted, which tries to remove menu.php entirely.

For adding extensions you need two rules: obviously the one to add the extension, but you should have another rule to remove it if someone tries to go to /menu.php because of SEO stuff.

Here is the second one because it's a little more interesting. Try coming up with the first one yourself.

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php($|\?)
RewriteRule (.*)\.php$ $1 [L,R=301]

 

8 minutes ago, requinix said:

Not this time 😁 This specific rule really is about removing the "menu.php". The other rule (which you haven't shown) is about adding it.

So sometimes with mod_rewrites you are adding to a URL and other times you are removing from the URL?

 

 

8 minutes ago, requinix said:

For adding extensions you need two rules: obviously the one to add the extension, but you should have another rule to remove it if someone tries to go to /menu.php because of SEO stuff.

Here is the second one because it's a little more interesting. Try coming up with the first one yourself.


RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php($|\?)
RewriteRule (.*)\.php$ $1 [L,R=301]

 

Had a noisy neighbor last night and I got a whopping 3 hours of sleep.  Am ready to pass out, so not such a great time to learn mod_rewrites, but I appreciate your help nonetheless!!

If I want to go from /client1/menu to /client1/menu.php then how about...

	RewriteCond ???????
	RewriteRule client1/menu$ client1/menu.php [R=301][L]
	

 

Can you explain your code?

RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} \.php($|\?)
RewriteRule (.*)\.php$ $1 [L,R=301]

 

The first line check if the request is not a file?  Like /client/menu

The second line checks what comes after the hostname and looks for a .php ending?  What is ($|\?)

The third line takes everything after the hostname and before a .php ending and strips off the .php ending?

2 minutes ago, SaranacLake said:

So sometimes with mod_rewrites you are adding to a URL and other times you are removing from the URL?

Yeah. When dealing with this stuff you need to think in terms of what you want the server to do, not the user experience. Because they aren't always the same.

2 minutes ago, SaranacLake said:

If I want to go from /client1/menu to /client1/menu.php then how about...

Do you want to support other .php files?

2 minutes ago, SaranacLake said:

The first line check if the request is not a file?  Like /client/menu

Is a file.

2 minutes ago, SaranacLake said:

The second line checks what comes after the hostname and looks for a .php ending?  What is ($|\?)

$ is end of input (end of the request URI). \? is a question mark. The purpose is to make sure that the request URI, which includes the query string, contains a .php at the end.

2 minutes ago, SaranacLake said:

The third line takes everything after the hostname and before a .php ending and strips off the .php ending?

Yes.

16 minutes ago, requinix said:

Depends on the answer to the "do you want to support other .php files" question I asked earlier.

 

My original intent was to allow a pretty URL in the address bar and then redirect to a formal menu.php URL.

If a user hacked the address bar and typed in "menu.php" then it would be nice if my mod_rewrite supported this.

For this particular script, I will not have any query strings.

16 minutes ago, requinix said:

Okay. But do you want to support other .php files?

I will not ask a fourth time.

I thought I answered your question.

This mini website will only support php files.

I want a pretty URL always (e.g. "/client1/menu") but if a user modified the URL to (e.g. "/client1/menu.php") then things should still work.  (That is what I thought you were asking.)

This site is purely PHP as is my other website.  No HTML or TXT or whatever.

Does that answer your question?

 

 

10 minutes ago, requinix said:

Think of another .php file you have on the site. Pick any one. Do you want to be able to access that one without the .php extension too?

This simple photo site that I am *trying* to build for my co-workers is a total of 7 pages...

	index.php
	menu.php
	photo-gallery.php
	photo-details.php
	access-denied.php
	page-not-found.php
	config.php
	

 

I guess I could hide the .php for my two error scripts in addition to menu.php

 

Edited by SaranacLake

That seems to work.

What does it say?

1.) Requested path is not a Directory?

2.) Requested path is not a File?

3.) Requested path is a file ending in .php?

4.) Are #2 and #3 an OR condition?

5.) Should .* be in parentheses?  (.*)

6.) What is $0.php ?

 

18 hours ago, SaranacLake said:

What does it say?

RewriteCond %{REQUEST_FILENAME} !-d 

Ensures the request does not map to a valid directory.

RewriteCond %{REQUEST_FILENAME} !-f 

Ensures the request does not map to a valid file

RewriteCond %{REQUEST_FILENAME}.php -f 

Ensures that if you add .php to the requested location, it does map to a valid file.

RewriteRule .* $0.php [L]

If all the above conditions are true, then this matches the entire URL (the .*) and rewrites it to include the .php suffix.

18 hours ago, SaranacLake said:

5.) Should .* be in parentheses?  (.*)

6.) What is $0.php ?

The parentheses create a group which can be referenced in the substitution.  In this instance they are not needed because you want to refer to the entire match which is already handled by $0 which is an automatic reference to the matching input string as a whole.  If you wanted to match only a subset of the input string, then you'd use parentheses and $1, $2, etc.

  • Like 1
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.