KhaledBawz Posted August 4, 2013 Share Posted August 4, 2013 Hello,I'm working on script, and want to set links to be as following:www.mysite.com/sign-up.phptowww.mysite.com/sign-upI found this code and works for me. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(.+)$ $1.php [L,QSA] BUT if I add a slash at the end of link, ex: www.mysite.com/sign-up/it shows me this error Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Q2- if the user try to access any php extension, I want him to redirect to same link without extension EX: www.mysite.com/sign-up.php to www.mysite.com/sign-up Thank you Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/ Share on other sites More sharing options...
requinix Posted August 4, 2013 Share Posted August 4, 2013 I take it the extensionless links didn't work before you put that rewriting in? Did you see how it said More information about this error may be available in the server error log.So... have you looked in the server error log for more information about the error? Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443444 Share on other sites More sharing options...
jazzman1 Posted August 5, 2013 Share Posted August 5, 2013 You should add the slash at the end as an option in the URL. Change: RewriteRule ^(.+)$ $1.php [L,QSA] to RewriteRule ^(.+)/?$ $1.php [L,QSA] Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443459 Share on other sites More sharing options...
KhaledBawz Posted August 5, 2013 Author Share Posted August 5, 2013 I take it the extensionless links didn't work before you put that rewriting in? Did you see how it said So... have you looked in the server error log for more information about the error? here is the error log [Mon Aug 05 13:50:32.523228 2013] [authz_core:error] [pid 6024:tid 1636] [client ::1:62516] AH01630: client denied by server configuration: C:/wamp/www/adsgate/.htaccess, referer: http://localhost/ [Mon Aug 05 13:50:40.452223 2013] [core:error] [pid 6024:tid 1636] [client ::1:62516] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. [Mon Aug 05 13:50:42.275454 2013] [core:error] [pid 6024:tid 1636] [client ::1:62523] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. [Mon Aug 05 13:51:51.726193 2013] [core:error] [pid 6024:tid 1632] [client ::1:62670] AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443500 Share on other sites More sharing options...
KhaledBawz Posted August 5, 2013 Author Share Posted August 5, 2013 You should add the slash at the end as an option in the URL. Change: RewriteRule ^(.+)$ $1.php [L,QSA] to RewriteRule ^(.+)/?$ $1.php [L,QSA] thanks for your reply, I tried that but still shows me error Internal Server Error The server encountered an internal error or misconfiguration and was unable to complete your request. Please contact the server administrator at admin@example.com to inform them of the time this error occurred, and the actions you performed just before this error. More information about this error may be available in the server error log. Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443501 Share on other sites More sharing options...
jazzman1 Posted August 5, 2013 Share Posted August 5, 2013 (edited) This regExp accept only letters, numbers and underscores inside file's name. RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([a-zA-Z_0-9]+)/?$ $1\.php [L,QSA] PS: The slash symbol is optional. Edited August 5, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443528 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 thanks jazzman1, it works now with slash at the end but the css files doesn't work so the page is something like there is no css included files. Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443628 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 (edited) Because, the "/" symbol has a special meaning for apache server. Just apply a new rule and remove it from the url if it exists. Try, RewriteEngine On RewriteBase /work_dir RewriteRule (.*)/$ $1 [R,L] RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^([a-zA-Z0-9_]+)$ $1\.php [L,QSA] Note: Replace /work_dir with your current working directory! Edited August 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443666 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 (edited) thanks, there is some small prblem, if my file name contain "-" then wil show the file not found, otherwise works fine. note: I would like to use the slash, I need it for user's profile ex: www.mydomain.com/profile/username/ Edited August 6, 2013 by KhaledBawz Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443676 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 (edited) So, the solution above could cause some problems. For example if the php file contains the same name as a sub-directory, let's say - /bash.php and /bash/ (it's a directory). To avoid that, make sure that the name of the directory has not the same name as the file that you want to rewrite. So, change that: RewriteEngine On RewriteBase /work_dir RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule (.*)/$ $1 [R,L] RewriteRule ^([a-zA-Z0-9_]+)$ $1\.php [L,QSA] If the file itself contains a "-" character(s), just add it to the regExp. RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA] Edited August 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443679 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 (edited) thanks jazzman1 everything is works fine. ex: www.mydomain.com/profile/username/ the above link is working but again that page will not include css all links in above link will be like I add this code RewriteRule ^profile/(.*)$ profile.php?username=$1 [L] www.mydomain.com/profile/sign-up and it should be www.mydomain.com/sign-up I'm sorry I asked you so many questions Edited August 6, 2013 by KhaledBawz Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443680 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 I'm not sure that you properly undestand the difference b/w a file and a directory. In the example above "www.mydomain.com/profile/username/", is the username a file or a directory? Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443681 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 it's a file I have profile.php and from this file I will get "username" by using this code $_GET['username']; and I add this code to my .htaccess RewriteRule ^profile/(.*)$ profile.php?username=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443683 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 I think you want to totaly confuse apache and yourself too The userName is not a view file, everything after a "?" symbol is "QUERY_STRING", that's not a file nor a dir. Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443684 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 Solution: Inside your view file, in that example profile.php, create a pretty link. Something like: <a href=./$userName> Username</a> And apply a third rule inside .htaccess file. RewriteRule ^/?(.*?)$ profile.php?username=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443686 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 I'm not that expert in php, but want to show you my idea I would like to get the username form the link itself, if there a username in database has the same name in link then I'll print all his information, otherwise I'll show there no a user with that name. all the codes to do that should be in profile.php the code you put it in your last comment doesn't work. it keep shows profle.php at all time, if I click on sign-up.php it also print profile.php this code is working except css style RewriteRule ^profile/(.*)$ profile.php?username=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443694 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 (edited) Sorry for the delay, but I should have to pay the bills So, inside profile.php create this ( just for test) profile.php <link rel="stylesheet" href="stylesheets/style.css" type="text/css"> <?php if(!isset($_GET['username'])) {echo '<a href=./profile/jazzman>UserName</a>';} else { echo '<pre>'.print_r($_GET, true).'</pre>'; } The user name should be jazzman and will come dynamically from DB .htaccess file RewriteEngine On RewriteBase /work_dir RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule (.*)/$ $1 [R,L] RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA] RewriteRule ^/?profile/([a-zA-Z_]+)$ profile.php?username=$1 [L,R=302] Run that script and tell me what result you get. Edited August 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443707 Share on other sites More sharing options...
KhaledBawz Posted August 6, 2013 Author Share Posted August 6, 2013 link: http://localhost/adsgate/profile.php?username=abc result: Array([username] => abc) so it works fine but the link is not like what I want Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443730 Share on other sites More sharing options...
jazzman1 Posted August 6, 2013 Share Posted August 6, 2013 (edited) Hey KhaledBawz, so, the best way in that case is to create a new directory inside "/" web root. For example, for all css stylesheets files you would create a directory with name stylesheets. And know the path to this directory is gonna be /stylesheets/style.css not ./stylesheets/style.css That's all. Try that now: profile.php <link rel="stylesheet" href="/stylesheets/style.css" type="text/css"> <?php if(!isset($_GET['username'])) {echo '<a href=./profile/jazzman>UserName</a>';} else { echo '<pre>'.print_r($_GET, true).'</pre>'; } .htaccess RewriteEngine On RewriteBase /work_dir RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule (.*)/$ $1 [R,L] RewriteRule ^([a-zA-Z0-9_-]+)$ $1\.php [L,QSA] RewriteRule ^/?profile/([a-zA-Z_]+)$ profile.php?username=$1 [L] If you have images doing on the same way and logic. Edited August 6, 2013 by jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443749 Share on other sites More sharing options...
KhaledBawz Posted August 7, 2013 Author Share Posted August 7, 2013 I appreciate your help but I have the same problem, I gave up with there links. I'll use the default links thank you Jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443801 Share on other sites More sharing options...
jazzman1 Posted August 7, 2013 Share Posted August 7, 2013 I appreciate your help but I have the same problem, I gave up with there links. I'll use the default links thank you Jazzman1 No, it's imposibble. You are doing something wrong. The path to the stylesheets directory is absolutely and independent from your project directory. Do you know what a web root folder is? Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443808 Share on other sites More sharing options...
KhaledBawz Posted August 7, 2013 Author Share Posted August 7, 2013 Okay, the stylesheets is in follder called ''stylesheets" and all php files inside folder called 'adsgate'. so both stylesheets and adsgate is in root ('www') Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443817 Share on other sites More sharing options...
jazzman1 Posted August 7, 2013 Share Posted August 7, 2013 No, no and no! The structure shoud be: / - web root (www) /stylesheets - your css files to this project /project - your project directory Now, do you understand me? Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443820 Share on other sites More sharing options...
KhaledBawz Posted August 7, 2013 Author Share Posted August 7, 2013 yes but I found another solution, see: http://www.webconfs.com/url-rewriting-tool.php I changed my links to something else, thank you jazzman1 Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443826 Share on other sites More sharing options...
jazzman1 Posted August 7, 2013 Share Posted August 7, 2013 You have to be kidding Do you realy understand what this tool does? Quote Link to comment https://forums.phpfreaks.com/topic/280825-change-links-using-htaccess/#findComment-1443843 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.