2017-09-28 35 views
3

只有有两个文件在我的http://www.example.com/members/目录:一个.htaccess文件和index.php文件:获取URL的最新部分作为输入到PHP文件


的内容该.htaccess文件:

RewriteEngine on 
RewriteRule (.*) index.php?code=$1 

项的index.php文件的内容:

<?php var_dump($_GET); 

我也有一大堆的URL在 “http://www.example.com/members/nnnn” 的格式。我只想在我的PHP文件中获得nnnn部分,而URL 完好

但是现在,如果我请求的网址为http://www.example.com/members/13,我想在我的index.php文件中获得13;而我得到的却是:

array(1) { ["code"]=> string(9) "index.php" } 

13不存在!那么,如何更改.htaccess代码以获得所需的行为?

感谢提前:)

+2

我建议看看$ _SERVER数组,并检查哪些元素具有网址。每次安装可能会有所不同。但$ _SERVER ['REQUEST_URI']可能是个不错的选择。然后使用strrpos()获取最后一个斜杠(不是尾随的)。这也可以做USINT $ _GET [“代码”]但是这仅仅是额外的努力 –

+0

顺便说一句:如果你的规则后省略[R]或[L],它不是默认为[R]? –

+1

@IvoP哇!在'$ _SERVER'含有比我预期的:)嗯,所以现在,听起来像我甚至都不需要在'.htaccess'文件'的index.php?代码= $ 1',谢谢你,你更真棒:) – goodUser

回答

2

试试这个在.htaccess文件:

确保您.htaccess和索引文件是在同一文件夹中。

# set directory index 
DirectoryIndex index.php 

# No directory listings 
IndexIgnore * 

<IfModule mod_rewrite.c> 
    # Uncomment the following line if your webserver's URL is not directly related to physical file paths. 
    # Also uncomment it if you are on 1&1 hosting 
    #RewriteBase/

    Options +FollowSymlinks 
    RewriteEngine On 
    RewriteCond %{REQUEST_FILENAME} !-f 
    RewriteCond %{REQUEST_FILENAME} ! 
    RewriteRule ^([a-zA-Z0-9]+)/?([a-zA-Z0-9]*)?/?([a-zA-Z0-9]*)?$ index.php?code=$1&code2=$2&code3=$3 [NC,L] 
    RewriteRule ^$ index.php [L] 
</IfModule> 

如果你想的多的路径水平;然后,就在$号前添加下面的最后RewriteRule

/?([a-zA-Z0-9]*)? 

使用,网址:

www.example.com/members/13 

将变为:

www.example.com/members/index.php?code=13 
+0

感谢您的回复,我appricate它:)我忘了提及,我想要的URL完好无损! (我刚刚更新了问题:) – goodUser

+0

无论代码如何导致URL被修改;它也重定向到一个奇怪的URL:'/home/example/domains/example/public_html/members/index.php?code=13':o这些文件在共享主机上,BTW:\ – goodUser

+0

我编辑答案看看它 –

2

为了有更多的空间比使用我的评论above

我会写.htaccess文件:

RewriteEngine On 
# if given url does point to exsiting Dir or File: 
RewriteCond %{REQUEST_FILENAME} -d [OR] 
RewriteCond %{REQUEST_FILENAME} -f 
# Then do nothing and stop 
RewriteRule^- [L] 

# still here: so go to index.php to execute 
RewriteRule ^.* index.php [L,QSA] 

index.php,我会再检查$_SERVER['REQUEST_URI']找到的信息。我给你留下strrpos()substr()甚至explode()做你的事情。

+0

这真的很好:)和$ _SERVER ['REQUEST_URI']'解决我的问题,谢谢:) – goodUser