如何使用像Instagram这样的.htaccess重定向网址?

时间:2017-12-16 11:29:23

标签: .htaccess redirect

如果链接没有任何值,请重定向到index.php。

->

如果链接是www.example.com/post/value,请重定向到post.php。

BidirectionalIterator

如果链接仅包含值,则重定向到profile.php。

Example;
www.mydomain.com/     --> index.php

我的测试:

Example;
www.mydomain.com/post/cDfS58Q     --> post.php

但他们都去了post.php。

2 个答案:

答案 0 :(得分:1)

为每个方案设置重写规则,但没有任何值的链接除外。访问网站时打开的默认文件是index文件,因此您无需明确设置。

从最长URI开始到最小,因为较小URI的条件通常满足较大URI的条件。

尝试这样的事情:

# Mod Rewrite setup
Options +FollowSymlinks
RewriteEngine on
AddDefaultCharset UTF-8

# First enter the one with the longest URI 
RewriteCond %{REQUEST_URI} ^/?post/([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /post.php?data=%1 [NC,L]

# Then the one with only value
RewriteCond %{REQUEST_URI} ^/?([0-9a-zA-Z-_/.]+)/?$
RewriteRule ^(.*)$ /profile.php?data=%1 [NC,L]

答案 1 :(得分:0)

我以不同的方式做到了;

htaccess的

RewriteRule ^p/(.*)$ post.php?$1 [QSA,L]
RewriteRule ^([0-9a-zA-Z]+)$ index.php?$1 [QSA]
RewriteRule ^([0-9a-zA-Z]+)/$ index.php?$1 [QSA]

的index.php

<?php
if (!empty($_SERVER['QUERY_STRING'])) {

    //Profile.php will be included here.
    echo "You are in profile now.";

 }else {
    //Homepage.php will be included here.
    echo "You are in profile now.";
 }
?>

网址:www.localhost.com/adsasd

输出:You are in profile now.

网址:www.localhost.com /

输出:You are in homepage now.