2014-10-30 114 views
2

我正在使用这两行来重写任何URL,例如www.example.com/spyke(其中spyke是用户名,可以是任何用户名)..在.htaccess中重写URL(文件夹名称为自定义URL,文件夹名称为变量值)

RewriteCond %{REQUEST_URI} ^/([^/]+)? [NC] 

RewriteRule ^/([^/]+)? http://www.example.com/index.php?id=82&user=$1 [L,R=301] 

这种近乎工作,但该系统目前被重定向到URL是:

www.example.com/index.php?id=82&user=index.php

..而不是

www.mydomain.com/index.php?id=82&user=spyke

1)所以我想重写www.mydomain.com/spyke到www.example.com/index.php?id=82&user=spyke ..有没有人知道如何实现这个?

2)另外,我希望用户在地址栏中看到www.example.com/spyke而不是www.example.com/index.php?id=82&user=spyke - 这也可能吗?

谢谢!!!!!

罗埃尔

这里是我的(更新)htaccess文件BTW:

RewriteEngine叙述在

重写规则^/TYPO3 $ - [L] 重写规则^/TYPO3 /.*$ - [L]

RewriteCond%{REQUEST_FILENAME}!-f

的RewriteCond%{REQUEST_FILENAME}!-d

的RewriteCond%{REQUEST_FILENAME}!-l

重写规则。*的index.php

重写规则^ /([A-ZA-Z0-9_- ] +)? http://www.example.com/index.php?id=82&user= $ 1 [L,R = 301]

..

但这只是导致该链接: http://www.example.com/index.php?id=82&user=index

+1

如何将'user'翻译成'id'? – anubhava 2014-10-30 08:42:25

+1

id始终为82;它的“用户”需要变量 – 2014-10-30 09:02:25

回答

2

使用本htaccess的:

RewriteCond %{REQUEST_FILENAME} -f [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 

RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?id=82&user=$1 [L] 
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?id=82&user=$1 [L] 

当此网址:“www.example.com/spyke”要求

此htaccess加载“index.php?id = 82 & user = spyke”而不是!

这条线加载一个文件,如果存在!

RewriteCond %{REQUEST_FILENAME} -f [OR] 
    RewriteCond %{REQUEST_FILENAME} -l [OR] 
    RewriteCond %{REQUEST_FILENAME} -d 
    RewriteRule ^.*$ - [NC,L] 

如果请求的URL不是文件,然后加载“index.php?ID = 82 &用户= [字符串]”

+0

感谢您的贡献:) 但是,这似乎不起作用,当我输入example.com/theuser时,没有任何更改。我只收到文件/页面未找到错误。 顺便说一下,这个作品: RewriteRule^/([a-zA-Z0-9 _-] +)? http://www.example.com/index.php?id=82&user=$1 [L,R = 301] 但结果在www.example.com(..)&user = index而不是(..)&user =用户名 当我替换这个^ /([a-zA-Z0-9 _-] +)?由此^ /([a-zA-Z0-9 _-] +)$ - 替换?对于$ - 它不起作用(根本没有htaccess活动可言)。因此,出于某种原因,$在那个地方不适合我... – 2014-10-30 09:06:09

+0

@R_K:清除浏览器缓存并重新测试。 – anubhava 2014-10-30 10:09:12

+0

请在我的主要帖子/问题中看到我的(更新的)htaccess文件。这只会导致http://www.example.com/index.php?id=82&user=index而不是http://www.example.com/index.php?id=82&user=username – 2014-10-30 10:43:45

0

试试这个:

RewriteRule ^author/(.+)$ index.php?pagename=books&author=$1 

我在这里找到:Redirect number 5

在你的情况,你应该用:

RewriteRule ^(.+)$ index.php?id=82&user=$1