luke777 Posted December 19, 2012 Share Posted December 19, 2012 Hey, I'm having difficulty getting my htaccess working. I havnt dealt with apache for such a long time a forgot how much I sucked at it. RewriteEngine On RewriteRule ^old.php$ ^new.php [R,NC,L] This is my first step which I cant seem to get working, i've tried several variations : / I'm not sure if its my hosting or what (I'm with hostgator) Obviously the above should read new.php when user types mywebsite.com/old.php but url should stay the same which would be great. But that doesn't work for me : / So this is embarrasing, very short code and I'm getting nowhere but what am I doing wrong? I ultimatly want to make any requests in /banners/example-user-type for the request to be resent to /banners/createpage.php but url should stay the same as the user typed the first time. Any help goes without saying, you'll be a legend. More stuck than ive been in a long while with this Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/ Share on other sites More sharing options...
requinix Posted December 19, 2012 Share Posted December 19, 2012 1. I can never keep straight whether leading slashes are needed in a particular RewriteRule. 2. The [R] flag will redirect. Since you want the URL to remain unchanged, don't redirect. Also [NC] is probably unnecessary. 3. The replacement URL is not a regular expression - just a regular string. RewriteRule ^/?old\.php$ new.php [L] As for the banner thing, I take it "example-user-type" can be any arbitrary filename? It's a little harder since you need to include "the request must not actually exist as a file or directory" conditions. RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^/?banners/(.+) banners/createpage.php?banner=$1 [L] You may want to be more restrictive with the ".+" since that will allow absolutely anything, and you didn't explicitly say so but createpage.php may not need the ?banner= (as far as I know it might look at the original URL automatically). Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400391 Share on other sites More sharing options...
luke777 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) Hi requinix, thanks for for this my .htaccess definatly now reads RewriteEngine On RewriteRule ^/?old\.php$ new.php [L] and I also have new.php and old.php yet with this code im recieving a 404 error when i type /old.php and this htaccess code is implemented. I just cant get past this stage could you please check the code mabe its something to do with my account hostgator im not sure Edited December 19, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400396 Share on other sites More sharing options...
requinix Posted December 19, 2012 Share Posted December 19, 2012 You do have the new.php in place, right? What does your access log say the 404 was for? Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400400 Share on other sites More sharing options...
luke777 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) 183.88.244.104 223 /home/lwilson9/public_html/.htaccess a _ o r lwilson9 ftp 1 * c Wed Dec 19 12:54:27 2012 0 183.88.244.104 218 /home/lwilson9/public_html/.htaccess a _ i r lwilson9 ftp 1 * c Wed Dec 19 12:54:50 2012 0 183.88.244.104 226 /home/lwilson9/public_html/.htaccess a _ o r lwilson9 ftp 1 * c Wed Dec 19 12:55:09 2012 0 183.88.244.104 219 /home/lwilson9/public_html/.htaccess a _ i r lwilson9 ftp 1 * c Wed Dec 19 13:00:27 2012 0 183.88.244.61 222 /home/lwilson9/public_html/.htaccess a _ i r lwilson9 ftp 1 * c I just got this can you make head or tail of it? I logged onto cpanel to check access logs there too but couldnt find any, these are from ftp not sure if relevant yes new.php is 100% in place, i really dont get this.. Edited December 19, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400403 Share on other sites More sharing options...
luke777 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) The strangest thing is that when I remove the htaccess code new.php or old.php appears fine. new.php contains "new" old.php contains "old" so the htaccess must be doing something...Just giving me hostgator 404 error page for some reason when the htaccess code is in place though - i realise the code above is ftp logs, cpanel contains access logs but says nothing about a 404 for the new.php doesnt even log it at all while there is other logs there. Edited December 19, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400410 Share on other sites More sharing options...
requinix Posted December 19, 2012 Share Posted December 19, 2012 Stick the [R] flag back in. What happens now? Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400481 Share on other sites More sharing options...
luke777 Posted December 19, 2012 Author Share Posted December 19, 2012 (edited) hey requinix, i just stuck [R] without the L it didnt work with the L aswell. And Im getting a redirect that works. I spoke to hostgator support couldnt give me much info other than 'they dont support scripting errors' Im sure its just an issue with their hosting. I setup 2 free hosting account and it worked on them. So Im just going to have to change host i dont think there is any other solution is there? I need it to run without the address bar changing obviously just old.php to new with no change in url Edited December 19, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400492 Share on other sites More sharing options...
requinix Posted December 19, 2012 Share Posted December 19, 2012 1. Are you actually using "old.php" and "new.php", or are those placeholders for the real filenames? If so then what are those filenames? 2. Have you tried the banner one yet? Does that work? Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400499 Share on other sites More sharing options...
luke777 Posted December 20, 2012 Author Share Posted December 20, 2012 (edited) Yes I am using old.php and new.php they are real files in /public_html/ old.php contains <?php echo 'old'; ?> and new.php contains <?php echo 'new'; ?> I just tried the htaccess code for /banners also and got redirected to 404 when I typed mysite.com/banners/any-name into browser : / And of course createpage.php is in place to echo 'createpage' but i see only a 404 error still :| Edited December 20, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400561 Share on other sites More sharing options...
luke777 Posted December 20, 2012 Author Share Posted December 20, 2012 (edited) I now have old.php reading from new.php and createpage.php working also thanks very much! I had to add rewritebase /~wilson because i was running off an ip address this was the problem ! Edited December 20, 2012 by luke777 Quote Link to comment https://forums.phpfreaks.com/topic/272191-htaccess-read-but-dont-redirect/#findComment-1400595 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.