roldahayes Posted February 15, 2016 Share Posted February 15, 2016 Hi, Is it possible to dynamically change a variable depending on the URL of the page? i.e my code on the page is: $CODE = "somethinghere"; if the page url is www.domain.com/654321.php it chages it to $CODE = "654321"; I hope I've explained this correctly! Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/ Share on other sites More sharing options...
requinix Posted February 15, 2016 Share Posted February 15, 2016 Sure. It's called URL rewriting. What actually happens is requests for /654321.php transparently (user doesn't know) get rewritten to something like /page.php?code=654321. Then you can get your $CODE from the $_GET array. Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531128 Share on other sites More sharing options...
roldahayes Posted February 16, 2016 Author Share Posted February 16, 2016 Ok, so do I have to use .htaccess to set this up? I'm a little confused by different examples after searching URL rewriting. Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531159 Share on other sites More sharing options...
requinix Posted February 16, 2016 Share Posted February 16, 2016 If you have Apache and shared hosting (ie, you don't control the system) then yes, you'd use a .htaccess with mod_rewrite rules. Looks like RewriteEngine on # only match if the requested file doesn't exist RewriteCond %{REQUEST_FILENAME} !-f # only match if the requested directory doesn't exist RewriteCond %{REQUEST_FILENAME} !-d # rewrite 123.php to page.php?id=123 and stop processing RewriteRule ^(\d+)\.php$ page.php?id=$1 [L]Though the two RewriteConds probably aren't even necessary. Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531160 Share on other sites More sharing options...
roldahayes Posted February 16, 2016 Author Share Posted February 16, 2016 Is there a way to test this is working? I was thinking I could use: echo 'Display: ' . htmlspecialchars($_GET["id"]); But not sure if this is just not working or it is wrong? Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531161 Share on other sites More sharing options...
requinix Posted February 16, 2016 Share Posted February 16, 2016 You would test it by... putting the stuff in place, writing code, and seeing if it works. Did you put that stuff in a .htaccess file in your website directory? Is your local setup and your web host configured to allow using .htaccess files at all? Easy way to check that second question is to put asdfstuff in the .htaccess and seeing what happens to your site: if your site loads then .htaccess isn't being used and you need to deal with that, and if not then remove that stuff because it is being used. Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531179 Share on other sites More sharing options...
roldahayes Posted February 17, 2016 Author Share Posted February 17, 2016 Yes, the htaccess appears to be loading (I have it doing other things to different urls on the site) Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531206 Share on other sites More sharing options...
roldahayes Posted February 17, 2016 Author Share Posted February 17, 2016 I just want to make sure that I have explained myself correctly in my original post. My full URL's are like this: http://mysite.com/PPP/awr99-1322.php Within my code I have: $CODE = "awr99-1322"; (This then generates everything on my page to be working correctly) I need the $CODE="---------"; to be generated every time depending on url name. So for example, if the url was: http://mysite.com/PPP/awr100-1322.php Then the code would end up as, $CODE = "awr100-1322"; Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531207 Share on other sites More sharing options...
requinix Posted February 17, 2016 Share Posted February 17, 2016 Is awr100-1322.php an actual PHP file? Or what? What is the file with this $CODE stuff? Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531219 Share on other sites More sharing options...
roldahayes Posted February 18, 2016 Author Share Posted February 18, 2016 (edited) No, awr100-1322.php is the page URL. I want the code within the page to read the url and populate itself with the page url (minus the .php) i.e $CODE = "Page URL Here"; At the moment, I have to manually open the page and change the $CODE to what ever the page Url is - I want this to happen dynamically Edited February 18, 2016 by roldahayes Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531254 Share on other sites More sharing options...
requinix Posted February 18, 2016 Share Posted February 18, 2016 What is the name of the actual file containing the code? And did the .htaccess thing work for you or not? Quote Link to comment https://forums.phpfreaks.com/topic/300815-change-code-depending-on-url/#findComment-1531261 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.