hackalive Posted August 18, 2011 Share Posted August 18, 2011 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. Quote Link to comment Share on other sites More sharing options...
cags Posted August 18, 2011 Share Posted August 18, 2011 RewriteRule ^corporate/?$ /corporate.php [L] RewriteRule ^corporate/(\d+)/?$ /corporate/page$1.php [L] Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 18, 2011 Author Share Posted August 18, 2011 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 Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 18, 2011 Author Share Posted August 18, 2011 its probably going to require the use of a RewriteCond? Quote Link to comment Share on other sites More sharing options...
cags Posted August 18, 2011 Share Posted August 18, 2011 Just swtich the corporate in the first half for ([^/]+) and in the second half to $1 (and switch $1 to $2 in the second rule). Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 18, 2011 Author Share Posted August 18, 2011 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 Quote Link to comment Share on other sites More sharing options...
cags Posted August 18, 2011 Share Posted August 18, 2011 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. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 18, 2011 Author Share Posted August 18, 2011 Cheers cags, how can I exclude directories and also make root/corporate.php rewrite to croot/corporate not root/corporate/ also it is not rewriting root/corporate/5.php to root/corporate/5 for some reason thansk for all your help so far cags Quote Link to comment Share on other sites More sharing options...
cags Posted August 19, 2011 Share Posted August 19, 2011 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. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 cags, sorry to be a pain but I always said www.mydomain.com/corporate/page5.php to www.mydomain.com/corporate/5 Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 If someone could help me it would be much much appreciated. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 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 Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 even using part of the code you sent me Options +FollowSymLinks RewriteEngine on RewriteRule ^([^/]+)/?$ /$1.php [L] returns an Internal Error Quote Link to comment Share on other sites More sharing options...
Jumpy09 Posted August 19, 2011 Share Posted August 19, 2011 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. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 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? Quote Link to comment Share on other sites More sharing options...
Jumpy09 Posted August 19, 2011 Share Posted August 19, 2011 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. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 19, 2011 Author Share Posted August 19, 2011 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 Quote Link to comment Share on other sites More sharing options...
cags Posted August 19, 2011 Share Posted August 19, 2011 ... also it is not rewriting root/corporate/5.php to root/corporate/5 for some reason ... cags, sorry to be a pain but I always said www.mydomain.com/corporate/page5.php to www.mydomain.com/corporate/5 Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 20, 2011 Author Share Posted August 20, 2011 and your point is? Quote Link to comment Share on other sites More sharing options...
cags Posted August 20, 2011 Share Posted August 20, 2011 My point is, you moved the goal posts, then tried to claim they'd been there all along. Quote Link to comment Share on other sites More sharing options...
hackalive Posted August 20, 2011 Author Share Posted August 20, 2011 I fail to see that, but anyway ok cags Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.