开发者

Redirect to specific pages depending upon the url routing using htaccess file

I have the urls like

http://localhost/mis  
http://localhost/mis/newsletter.php  
http://localhost/mis/cms.php?mainid=65  
http://localhost/mis/news.php?mainid=93

Well I am using these urls for school section.

Now for the A-level section I have the following urls

http://localhost/mis/index.php?mode=0  
http://loca开发者_开发问答lhost/mis/newsletter.php?mode=0  
http://localhost/mis/cms.php?mainid=65&mode=0  
http://localhost/mis/news.php?mainid=93&mode=0

I want the above urls to be routed like

http://localhost/mis/a-level  
http://localhost/mis/a-level/newsletter.php  
http://localhost/mis/a-level/cms.php?mainid=65  
http://localhost/mis/a-level/news.php?mainid=93

Please help me out with these url routing via .htaccess file.

EDIT: The htaccess file is under mis folder

FILE STRUCTURE:

root>>
    mis>>
        .htaccess
        newsletter.php
        cms.php
        news.php


Try these in the .htaccess file inside the /mis folder.

# turning rewrite on
RewriteEngine On

# this will redirect: mis/a-level
RewriteRule ^a-level$ index.php?mode=0 [L]

# this will redirect all the rest
RewriteRule ^a-level/(.+)$ $1?mode=0 [QSA,L]

I think the lines like http://localhost/mis/cms.php?mainid=65&mode=0 are just copy-paste errors and you ment: http://localhost/mis/cms.php?mainid=65&mode=0

I have tested these rules. If they don't work on your system there must be some configuration issue. Are there other rewrite rules in your file? I not, try enabling rewrite logging to get some more information. This must be enabled in httpd.conf (they are not allowed in .htaccess).

RewriteLog D:\bence\wamp\logs\rewrite.log
RewriteLogLevel 3

My rewrite log for the url http://proba.test/mis/a-level/news.php?some=thing&other=33

127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add path info postfix: D:/bence/www/proba.test/mis/a-level -> D:/bence/www/proba.test/mis/a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level/news.php -> a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'a-level/news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add path info postfix: D:/bence/www/proba.test/mis/a-level -> D:/bence/www/proba.test/mis/a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level/news.php -> a-level/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'a-level/news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (2) [perdir D:/bence/www/proba.test/mis/] rewrite 'a-level/news.php' -> 'news.php?mode=0'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) split uri=news.php?mode=0 -> uri=news.php, args=mode=0&some=thing&other=33
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (3) [perdir D:/bence/www/proba.test/mis/] add per-dir prefix: news.php -> D:/bence/www/proba.test/mis/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (2) [perdir D:/bence/www/proba.test/mis/] strip document_root prefix: D:/bence/www/proba.test/mis/news.php -> /mis/news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#f67a88/initial] (1) [perdir D:/bence/www/proba.test/mis/] internal redirect with /mis/news.php [INTERNAL REDIRECT]
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/news.php -> news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/news.php -> news.php
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'news.php'
127.0.0.1 - - [11/Mar/2011:11:36:03 +0100] [proba.test/sid#713440][rid#253ab08/initial/redir#1] (1) [perdir D:/bence/www/proba.test/mis/] pass through D:/bence/www/proba.test/mis/news.php

And the log for: http://proba.test/mis/a-level

127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/a-level -> a-level
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'a-level'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (2) [perdir D:/bence/www/proba.test/mis/] rewrite 'a-level' -> 'index.php?mode=0'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) split uri=index.php?mode=0 -> uri=index.php, args=mode=0
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (3) [perdir D:/bence/www/proba.test/mis/] add per-dir prefix: index.php -> D:/bence/www/proba.test/mis/index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (2) [perdir D:/bence/www/proba.test/mis/] strip document_root prefix: D:/bence/www/proba.test/mis/index.php -> /mis/index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#25f6338/initial] (1) [perdir D:/bence/www/proba.test/mis/] internal redirect with /mis/index.php [INTERNAL REDIRECT]
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/index.php -> index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level$' to uri 'index.php'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] strip per-dir prefix: D:/bence/www/proba.test/mis/index.php -> index.php
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (3) [perdir D:/bence/www/proba.test/mis/] applying pattern '^a-level/(.+)$' to uri 'index.php'
127.0.0.1 - - [11/Mar/2011:11:39:29 +0100] [proba.test/sid#713440][rid#260c1d0/initial/redir#1] (1) [perdir D:/bence/www/proba.test/mis/] pass through D:/bence/www/proba.test/mis/index.php


RewriteEngine On

RewriteRule ^a-level$ a-level/
RewriteRule a-level/(.*)$ $1?mode=0 [QSA]

The first rule acts as support for the second one, since the second one needs the trailing slash. You could also add [R=301,L] to the first rule to redirect the user to the version with the slash. (For real, actual folders, Apache will do just that, automatically. Since this is not a "real" folder you'd have to do it yourself.)

The second rule will simply strip the a-level part of the URL. The secret sauce, however, that makes this dish so low-calorie, is the QSA part. QSA stands for query string append. It will simply jams any existing query string to the end of the URL.

This has the added benefit that you make no assumptions about the contents of the QS. If you'd ever need to add another query variable, apart from mainid, you don't need to modify the rule.

Edit: Or, like this, if the htaccess file is in the web root.

RewriteRule ^mis/a-level$ mis/a-level/
RewriteRule mis/a-level/(.*)$ /mis/$1?mode=0 [QSA]


RewriteEngine On
RewriteCond %{query_string} mode=0
RewriteRule mis/(.+?\.php)$ mis/a-level/$1 [QSA,L]


Got it...

RewriteCond %{QUERY_STRING} ^(.*)mode(.*)$
RewriteRule mis/(.*) /mis/a-level/$1?%1 [R]

Architecture for reference:

/root/
    .htaccess
    /mis/
        /a-level/
            newsletter.php
            cms.php
            news.php

localhost/mis/newsletter.php?mainid=65&mode=0

now redirects to

localhost/mis/a-level/cms.php?mainid=65 - - (MODE BE GONE) :-)

No php required for param/value stripping. Pretty neat trick, if you can keep your Cond/Rules and RegEx under control. (note: an almost identical Cond/Rule may need to go directly ahead of this solution to catch the index.php file)

You can actually change the param/values as well:

RewriteRule mis/(.*) /mis/a-level/$1?blue=9 [R]

Had some help http://wiki.apache.org/httpd/RewriteQueryString


I think you are looking at this the wrong way - your main problem is translating what the PHP code needs into something that is descriptive for the URL, rather than try and accomplish this via mod_rewrite, I suggest you reconsider your approach.

Using mode=0 is meaningless within the code, and is not obvious to anyone reading the code as to what it does. I suggest changing the code to accept mode=a-level instead. this will make your mod_rewrite rule far easier, and also make your code much easier to read.


Haven't tested it on a live server, but http://www.regextester.com/ shows these output what you need

Options +FollowSymLinks
RewriteEngine on

RewriteRule a-level/(.*)\.php\?mainid=(.*)$ /mis/$1.php?mainid=$2&mode=0
RewriteRule a-level/(.*)\.php$ /mis/$1.php?mode=0
RewriteRule a-level$ /mis/index.php?mode=0
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜