0

I have a php site and the URLs are displayed as follow:

http://www.example.com/cheap-call-single.php?country=ALBANIA

I want to re-write it, to display as:

http://www.example.com/cheap-international-calls-ALBANIA.php

I have searched many only generators and they all say to use:

RewriteEngine On
RewriteRule ^cheap-international-calls-([^/]*)\.php$ /cheap-call-single.php?country=$1 [L]

But this is not working for me, please note that the server has the correct Apache setting enabled.

Can someone please help me with the correct syntax?

2
  • But this is not working for me what does not work, when you access the first URL it doesn't convert or when you access the 2nd URL it doesn't work? The code you have posted should work when you access the 2nd URL. If none of the above kindly explain it being as descriptive as possible as we don't own crystal balls to foresee what problems you're having... Commented Sep 4, 2014 at 1:03
  • I have pasted the above into the htaccess file in the root of my folder and it makes no difference to how the URL is being displayed. Commented Sep 4, 2014 at 3:50

1 Answer 1

1

The rewriteRule you show does the inverse, it takes incoming url in the long form and translate it to the query string version (with ?).

Now the problem is what do you mean when you say: "I want to re-write it, to display as:"

The display and the rewrite are usually different things:

If the 'display' is the url seen by the user in his browser you have :

  • to push this way of writing urls in your application, so that the received HTML contains the right display, this has nothing to do with mod_rewrite
  • you may optionally perform HTTP redirections with mod_rewrite, so when you detect the old syntax (cheap-call-single.php?country=ALBANIA) you redirect the user on the right one, then the request is re-executed by the browser (and then you should have a cheap-international-calls-ALBANIA.php file on your server, else it's a final 404)
  • If you do not have this file on the server (so, what you have is cheap-call-single.php) then the exposed rewriteRule is right and back to step one, it's your application which should show the right url on the HTML side.

Now if your really have cheap-international-calls-ALBANIA.php and you want your application to rewrite incoming request using cheap-call-single.php to this file, based on query string parameters, you'll have some problems. Writing rules on the query string part of the request is always complex, query string arguments may appear in any order, may be written with urlencoding or not, etc. By default rewriteRules are not using the query string part.

This is something like:

RewriteCond %{REQUEST_FILENAME} cheap-call-single\.php [NC]
RewriteCond %{QUERY_STRING} (^|&|%26|%20)country(=|%3D)([^&]+) [NC]
RewriteRule .* /cheap-international-calls-%3.php [L,R]

Untested (not sure for the \.), and yet not managing the fact each letter in the country word would be urlencoded, and not managing the upcase of the country name. You would need a RewriteMap to transform it uppercase. But already have my headache, is this what you really need?

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you for the detailed reply. All I want to change is the display URL to assist with SEO. What I should have asked for was I have a php site and the URLs are displayed as follow: example.com/cheap-call-single.php?country=ALBANIA I want to display as: example.com/cheap-international-calls-ALBANIA.php Could you please advise the correct Syntax for this
@Lee Cokerill, so as stated in the answer, this is the job of your php application, not mod_rewrite, the display is managed there, when you generate the url for your links. And the rewriteRule you already have should work to rewrite theses incoming queries to the query string format
Please forgive my lack of understanding. So I cannot use a re-write in the htaccess for to change the url display to assist with seo rankings? The current url I wanted to change it's display so it is more apealing, I wanted to do this with a display re-write. Appreciate your time and help
No, the url that you users are using are the url that you are generating in your website. You can write them as you like, even in this SEO format, but that's not the job of mod_rewrite and apache. that's the job of your PHP application. search the word country in your code and you'll find places where YOU are generating theses urls. Make a timeline : generating html pages with seo url -> send html to browser -> user see url -> click on it -> new request comes to apache -> mod-rewrite decrypt url and associate it to a local file with a query string. <-- see?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.