简体   繁体   中英

mod_rewrite URL with many variables

I'm new to mod-rewrite and I have tried to mod-rewrite this url with no success.

URL structure like this :

http://mysite.com/script.php?id=15751890&xp=862297&wm=1721&ls=2725&he=63530&ks=23050&eath=53588&tk=10&ck=john&rk=37

I want to mod-rewrite it to :

http://mysite.com/script.php?id=15751890

Or :

http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37

I followed this http://wettone.com/code/clean-urls and this http://net.tutsplus.com/tutorials/other/a-deeper-look-at-mod_rewrite-for-apache/

But just can't do it write

What to type in htaccess file

Well, if you really want to write such a rewrite you should be able to use this in your htaccess file. There are probably better ways to handle the URL's however, I'm giving you what you asked for.

NOTE: I'm going off of the URL you provided.

RewriteEngine on
RewriteRule (.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*)/(.*) script.php?id=$1&xp=$2&wm=$3&ls=$4&he=$5&ks=$6&eath=$7&tk=$8&ck=$9&rk=$10&%{QUERY_STRING}$ [L]

So you can use this type of URL with that rewrite.

http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37

Refer to my answer to this question:

php, handle unique URLs

This will give you this:

http://mysite.com/15751890/862297/1721/2725/63530/23050/53588/10/john/37

Instead of trying to figure out every rewrite combination possible, you should learn the MVC routing method.

This way, you route everything to a PHP router, and then direct the request to controllers, etc, as needed.

HTACCESS to rewrite everything:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

In your PHP script, you can use: $_SERVER['REQUEST_URI'] to get the requested URI.

Example: http://example.com/foo/bar/95 would rewrite to index.php, and $_SERVER['REQUEST_URI'] would then be /foo/bar/95 - and then you route accordingly.

Typically, your URL structure would be like:

http://example.com/controller/action/param_1/param_2/param_3/etc

Whereas "action" is just another name for a controllers method. How you code it is up to you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM