Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/279461-htaccess-regular-expressions/
Share on other sites

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 by Csharp

 

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.

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=value2
And you want to look it nice, like this:

www.example.com/value/value2
Then 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=$2
Where $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 by Christian F.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.