Jump to content

Recommended Posts

Hi guys,

I need a rewrite that allows me to www.mydomain.com/corporate.php as www.mydomain.com/corporate but www.mydomain.com/corporate/page5.php to www.mydomain.com/corporate/5

 

and as a fail safe www.mydomain.com/corporate/ would go to www.mydomain.com/corporate if no index.php file was found in www.mydomain.com/corporate/

 

if anyone can help it would so much be appreciated.

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/
Share on other sites

thanks for that ... one thing I gorgot to list, it must work for all folders in the root and all files within a folder within the root

 

so

root/corporate.php = root/corporate AND root/corporate/

root/corporate/5.php = root/corporate/5

root/me.php = root/me

root/hi/index.php = root/hi AND root/hi/

etc, etc, etc

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1258984
Share on other sites

This is what I have

Options +FollowSymLinks
RewriteEngine on 
RewriteRule ^([^/]+)/?$ /$1.php [L]
RewriteRule ^([^/]+)/(\d+)/?$ /([^/]+)/page$2.php [L]

 

This is what I get

Internal Server Error

 

Also need to be able to exclude directories

 

and it may not be page.php it could be anything about.php page.php me.php so what do I do there

 

 

Thanks for all your fast responses and help so far cags

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1258989
Share on other sites

Either you don't have mod_rewrite enabled on your server, or you are getting an infinite redirect rule (which I don't think you should, but it's sounds like it's causing an internal redirect rather than rewrite.

 

RewriteEngine On
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^([^/.]+)/?$ /$1.php [L]

 

Should fix the first one, by redirecting only if the request is for a file/directory that doesn't exist. If you want to exclude directory names that don't exist then you will have to add another cond in to check if it matches that such as RewriteCond %{REQUEST_URI} !^foobar to exclude foobar from the redirect. As for your other question about page could be other stuff, I have no idea what you are after.

 

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259123
Share on other sites

You never mentioned in your original post that corporate/5 should go to corporate/5.php, you said it should go to corporate/page5.php. Either way you have enough information in this post to work out what you need. With a rudimentary understanding of regex you should be able to achieve the results you are after. Give it a fair shot, if you get stuck, come back with what you have and I'll maybe help some more.

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259296
Share on other sites

cags,

 

I have tried

 

Options +FollowSymLinks

RewriteEngine on

RewriteCond %{REQUEST_URI} !-f

RewriteCond %{REQUEST_URI} !-d

RewriteRule ^([^/]+)/(\d+)?$ /$1.php [L]

 

and others and cannot get it to work, it would genuinely appreciated if you could help me with the solution

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259357
Share on other sites

Try this:

 

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^/corporate$ /corporate.php [NC]

RewriteRule ^/corporate/$ /corporate.php [NC]

RewriteRule ^/corporate/(.+)$ /corporate/page$1.php [NC,L]

RewriteRule ^/corporate/(.+)/$ /corporate/page$1.php [NC,L]

 

As I mentioned in the Private Message there is a much nicer and easier method of doing this with a different type of system for your files in php.  This is if you are set on the current system you have.

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259457
Share on other sites

Hi Jumpy09 and cags ... in clear terms this is what I want to do

 

This is what the URL in the browser should look like # This is what thr file in the server is

 

http://www.mydomain.com/corporate # root/corporate.php

OR

http://www.mydomain.com/corporate/ # root/corporate.php

(This should fail safe also and check if there is root/corporate/index.php and use that if it exists)

 

http://www.mydomain.com/corporate/ # root/corporate.php

OR

http://www.mydomain.com/corporate/ # root/corporate/index.php

 

http://www.mydomain.com/corporate/bios # root/corporate/bios.php

OR

http://www.mydomain.com/corporate/bios/ # root/corporate/bios.php

 

Should always fail safe and check against if folder e.g., bios exists andf has index.php

 

make sense?

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259462
Share on other sites

Fail safe in htaccess would require rewriting the url and then rechecking it, which would result in a ton of loops.

 

Options +FollowSymLinks

RewriteEngine on

RewriteRule ^/corporate?$ /root/corporate.php [NC]

RewriteRule ^/corporate/?$ /root/corporate/index.php [NC]

RewriteRule ^/corporate/bios?$ /root/corporate/bios.php [NC]

RewriteRule ^/corporate/bios/?$ /root/corporate/bios.php [NC,L]

 

You cannot have /corporate/ direct to two different places.  You really should consider revamping the internal system to accompany a better solution for page calls.  I personally do not call anything past the root directory.  If something belongs to corporate, all of the pages would be called from corporate.php with a ?page= modifier.  This makes things much easier, and not so complex or having to deal with fail safes.  Mod_Rewrite isn't meant for fail safes, it is meant only if you have the system set up and you know what you are wanting to rewrite for use on the server.

 

If you need a fail safe, the folder schema isn't designed effectively.  I am sorry, but I do not think what you are wanting is possible as I previously mentioned fail safes would require multiple redirect loops which will result in a 500 Internal Server Error.

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259471
Share on other sites

Change of plan ... and please note corporate etc are examples and the rewrite needs to automatically deal with ANY

 

so http://www.mydomain.com/corporate/ would rewrite to http://www.mydomain.com/corporate which would be http://www.mydomain.com/corporate/index.php

 

and http://www.mydomain.com/corporate/corporate/bios/ rewtite to http://www.mydomain.com/corporate/bios which would be http://www.mydomain.com/corporate/bios.php

Link to comment
https://forums.phpfreaks.com/topic/245105-nice-urls/#findComment-1259488
Share on other sites

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.