MarioApprentice Posted June 22, 2013 Share Posted June 22, 2013 Hello everybody. My problem is .htaccess and the replacement of url-s. I have read articles, tutorials and code snippets all afternoon and am still vague about the rules that has to be in a .htaccess regular expression. My doubts are how does it work? Does the url www.example.php/SomeReplacedName/AnotherOne has to be in a <a href=''> or does the does www.example.php?example=value%another=value2? Let's say I have a link in my page (from localhost) that looks like this <a href=/localhost/examplePage/example.php?var=value&var2=value2> Then, .htacces shoud look like this Options +FollowSymLinks RewriteEngine On RewriteRule example\.php$ ReplacementText right? But it doesn't work. On the other hand, if the link in the page looks like this <a href='/localhost/examplePage/ReplacementText> The .htaccess works with this RewriteRule ReplacementText$ example.php and the browser url display the correct url like this-> localhost/examplePage/ReplacementText with the example.php acutally being called. I know there is abundance of documentation on this subject on the web, expecially on appace documentation (that is to specific and hard for a simple problem like this), but i really couldn't find a clear explanation how this shoul work. Is .htaccess taking the url from the browser adress page or the <a> element? On what side of the .htaccess syntax should be the replacement text and on what side the actual url of the document that is in the source code? Thank you in advance for all your answers. Quote Link to comment https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/ Share on other sites More sharing options...
Csharp Posted June 22, 2013 Share Posted June 22, 2013 (edited) Apache rewrites requested URL, so if you add a rewrite condition your URLs won't automatically be changed. On the other side using (A-Za-z0-9_-)+ will match most 'normal strings' so you could probably use something like this: RewriteRule (a-zA-Z0-9_-)+$ example.php?var=$1 Edited June 22, 2013 by Csharp Quote Link to comment https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/#findComment-1437415 Share on other sites More sharing options...
MarioApprentice Posted June 22, 2013 Author Share Posted June 22, 2013 Apache rewrites requested URL, so if you add a rewrite condition your URLs won't automatically be changed. On the other side using (A-Za-z0-9_-)+ will match most 'normal strings' so you could probably use something like this: RewriteRule (a-zA-Z0-9_-)+$ example.php?var=$1 That still doesn't solve my problem. Lets say i have a url on my page inside <a>. That url is localhost/someFolder/myPage.php or www.example.php/index.php, whatever you fancy. I would like to make it look like this www.example.php/replacementText with this code RewriteRule index.php$ ReplacementText I mean, i don't know if it has to be like this. I want to replace the SEO ugly www.example.php?somevar=value&somevar2=value2 into www.example.php/Seo-acceptable/Seo-acceptable2. Quote Link to comment https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/#findComment-1437418 Share on other sites More sharing options...
Christian F. Posted June 22, 2013 Share Posted June 22, 2013 (edited) To give a better explanation of what URL rewriting is about: It takes an (otherwise) invalid URL request from the client, and changes it to something that is valid for the web server.Now, the trick is that the request comes from the client. Which means that the URLs you use in the HTML code needs to be of the "nice" variant. Otherwise the client won't know about them, and just use the plain old ones. So, if the valid/proper URL would be this one: www.example.com/index.php?example=value&another=value2And you want to look it nice, like this: www.example.com/value/value2Then you need to make a rule to rewrite it from the invalid version, to the proper target URI. RewriteRule ^([\w]+)/([\w]+)$ index.php?example=$1&another=$2Where $1 and $2 are what's captured by the character groups inside of the parentheses. This will tell the server that when the client is asking for "/something/other", it is really asking for "index.php?example=something&another=other". There are some other details to this, such as flags to ensure that it only matches when there are no existing files. All of which can be read about in the Apache docs. Edited June 22, 2013 by Christian F. Quote Link to comment https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/#findComment-1437441 Share on other sites More sharing options...
Csharp Posted June 23, 2013 Share Posted June 23, 2013 That still doesn't solve my problem. Lets say i have a url on my page inside <a>. That url is localhost/someFolder/myPage.php or www.example.php/index.php, whatever you fancy. I would like to make it look like this www.example.php/replacementText with this code RewriteRule index.php$ ReplacementText I mean, i don't know if it has to be like this. I want to replace the SEO ugly www.example.php?somevar=value&somevar2=value2 into www.example.php/Seo-acceptable/Seo-acceptable2. The point is that this just rewrites the requests. In order to change the URL's your app generates you need to change them in your code. Quote Link to comment https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/#findComment-1437539 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.