2

I have a link like below:

In /country/search.php

<a href="<?php echo 'index.php?departments='.$value['department_id'].'&towns='.$value['town_id'].'&type='.$value['type_id'].'&id='.$value['id'] ?>">

When I use the below .htaccess code, it does nothing:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L] 

It does in the tool:

http://example.com/0/0/4/122

... but when I click that link it shows old URL again.

What is the problem here?

I'm generating that .htaccess code using this tool: http://www.generateit.net/mod-rewrite/

4
  • Your link already looks like the request you want to make - so what do you want to do? Commented Sep 21, 2013 at 16:25
  • yes.it does @kingkero.I want to make it like example.com/0/0/4/122 but htaccess code not working Commented Sep 21, 2013 at 16:26
  • htaccess code only rewrites your urls so that requests end up at the right place. the links you generate with your application are not affected by htaccess. You have to format it the right way yourself. Commented Sep 21, 2013 at 16:28
  • @rbtux can you guide me how to do it with me php showing some example please Commented Sep 21, 2013 at 16:29

3 Answers 3

2

Replace your code with this:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# external redirect from actual URL to pretty URL
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(index\.php|)\?departments=([^\s&]*)&towns=([^\s&]*)&type=([^\s&]*)&id=([^\s&]*) [NC]
RewriteRule ^ %1/%2/%3/%4/%5? [R=301,L]

# internal forward from pretty URL to actual URL
RewriteRule ^([^/]*)/([^/]*)/([^/]*)/([^/]*)/?$ /country/index.php?departments=$1&towns=$2&type=$3&id=$4 [L,QSA]
Sign up to request clarification or add additional context in comments.

Comments

0

I think you misunderstood what the rewrite rules are trying to achieve in this context.

F.e. this rewrite rule

RewriteRule ^([^/]*)/([^/]*)$ /app.php?method=$1&arg=$2

Will ensure that a request to http://example.com/mymethod/myarg will be rewritten to

http://example.com/app.php?method=mymethod&arg=myarg

When you generate links from within your application you should know how you contstructed the rewrite rules and you have to build the link accordingly.

Comments

-1

Original URL:

href="news.php?state="<?php echo $sql_res['state']?>"&country="<?php echo $sql_res['country']?>

Ideal URL:

/India/andhra%20Pradesh

And .htaccess code:

RewriteEngine On 

RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

1 Comment

php code <?php echo $sql_res['country']?>/<?php echo $sql_res['state']?> .htaccess code RewriteRule ^([^/]*)/([^/]*)$ /news.php?country=$1&state=$2 [L]

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.