PHP.htaccess & # 8594; pretty url (in reverse order)

I know how to rewrite a URL, for example: www.example.com/index.php?id=1&cat=3 to www.example.com/1/3/ (or something else). I know it.

What I don't know is how on earth to change all of my links on all pages to link to pretty URLs. All links to my sites are old fashion ( <a href="index.php?id=1&cat=2"> ), and there are many of them.

I ask if anyone has an idea or know how to automatically redirect this pretty url if the user clicks on index.php? id = 1. (Almost like this Stackoverflow site, if you change the title in the URL).

So my presumption ...

  • Use .htaccess to read index.php? id = 1 & cat = 2 to rewrite the index / 1/3, which itself again interprets (weird)

  • php redirect file that htaccess overwrites back to the original ...

Conclusion: change <a href="index.php?id=1&....."> automatically to index/1/2




solvable

 Options +FollowSymLinks RewriteEngine On RewriteBase / ################################## # This turns index.php?id=1&cat=2 into index/1/2 and then back 'transparent' into index.php?id=1&cat=2 if you have old fashioned # links in your site and don't want to change them :) # Avoid mod_rewrite infinite loops # This is critical if you want to use this code RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] # Hard-rewrite ("[R]") to "friendly" URL. # Needs RewriteCond to match original querystring. # Uses "?" in target to remove original querystring, # and "%n" backrefs to move its components. # Target must be a full path as it a hard-rewrite. RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$ RewriteRule ^index.php$ http://localhost/index/%1/%2/? [L,R] # Soft-rewrite from "friendly" URL to "real" URL. # Transparent to browser. # Won't re-trigger the above rewrite, though I'm # not really sure why! The order of the rules # doesn't seem to make a difference. RewriteRule ^index/(\d+)/(\d+)/$ index.php?id=$1&cat=$2 [L] 
+5
php friendly-url mod-rewrite rewrite
Apr 6 2018-11-21T00:
source share
2 answers
 RewriteEngine on # Prevents browser looping, which does seem # to occur in some specific scenarios. Can't # explain the mechanics of this problem in # detail, but there we go. RewriteCond %{ENV:REDIRECT_STATUS} 200 RewriteRule .* - [L] # Hard-rewrite ("[R]") to "friendly" URL. # Needs RewriteCond to match original querystring. # Uses "?" in target to remove original querystring, # and "%n" backrefs to move its components. # Target must be a full path as it a hard-rewrite. RewriteCond %{QUERY_STRING} ^id=(\d+)&cat=(\d+)$ RewriteRule ^index\.php$ http://example.com/index/%1/%2/? [L,R] # Soft-rewrite from "friendly" URL to "real" URL. # Transparent to browser. RewriteRule ^index/(\d+)/(\d+)/$ /index.php?id=$1&cat=$2 

Of course, ideally, you just fix your links, and then you only need soft-rewrite. :)

Tested with Apache / 2.2.3. I think I coined the terms "hard-rewrite" and "soft-rewrite".

+2
Apr 6 2018-11-11T00:
source share

Why not just change the index.php file to do this? Theoretically, you could do a little more error checking this way, allowing the variables to be in any order and still be redirected to the correct final location.

 <?php // Permanent redirection header("HTTP/1.1 301 Moved Permanently"); header("Location: http://www.example.com/{$_GET['id']}/{$_GET['cat']}"); 

I did not make any mistakes here, but wanted to give a basic example.

On the other hand, I assume that this is adding functionality to the index.php file, which you then want to use for your application, so perhaps this will lead to confusing functionality in the code.

0
Apr 6 2018-11-11T00:
source share



All Articles