我的网址是xyz.com/songs.php?lan=3&movie=198
,
我想向xyz.com/moviename.html
展示,.htacces..可以吗? 请帮助我
在我的 .htaccess 文件中,我使用 -
RewriteRule ^([^/.]+)/([^/.]+)$ songs.php?lan=$1&movie=$2 [QSA,L]
它的显示 - xyz.com/telugu/moviename
但我想展示xyz.com/moviename.html
请帮助我 对此是全新的
在这种情况下,您可以像CMS和框架所做的那样做:
首先将所有请求重定向到索引.php在 .htaccess 中:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{QUERY_STRING} base64_encode[^(]*([^)]*) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|[|%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|[|%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|.(php|html?|feed|pdf|vcf|raw|xml|jpg|ajx))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
在此之后,所有请求都将重定向到您的索引.php例如 DOMAIN/a.html 和 DOMAIN/a/b.html 和...
此时 $_SERVER['REQUEST_URI'] 是您请求的地址,例如/a.html您可以根据当前 URL 决定应显示哪些数据。
如果你愿意
xyz.com/moviename.html
然后你需要为每部电影写一个规则。
RewriteRule ^moviename.html songs.php?lan=3&movie-123
RewriteRule ^another-moviename.html songs.php?lan=4&movie-124
一种更简单的方法是在 URL 中的某个位置包含movie_id:
# Catch xyz.com/123/the-movie-title
RewriteRule ^([0-9]+)/([^/.]+).html songs.php?lan=3&movie=$2
这种方式可以捕获所有电影,而无需为每个电影制定特定规则。然后,您可以在代码中检查 ID 是否有效。您还可以抓取电影标题并检查它与 URL 匹配,如果没有,则根据电影 ID 重定向到正确的标题。
鉴于您的评论,我认为您应该尝试不同的方法。
更新您的 .htaccess 以通过单个文件(索引.php)路由所有请求。
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?route=$1 [L,QSA]
然后在索引中.php您可以执行以下操作(请注意,这需要以安全的方式实现,下面是一个粗略的概述):
<?php
// Copy route into variable.
$route = $_GET['route'];
$slugs = array_filter(explode('/', $route));
// Check there is a slug. e.g. a-great-movie
if(count($slugs) > 0) {
// Perform a SQL/Database query here so see if $slug[0] matches a movie.
// Oviously do this with you database of chose and sanitise input.
// e.g. SELECT id FROM movies WHERE slug = $slug[0]
// Check a match was found if so load your page content.
if($result->rowCount() > 0 ) {
// Show page content.
}
else {
// Show a 404 page?
}
}
else {
// Show homepage.
}
完成上述操作后,您现在可以转到以下网址:
xyz.com/a-great-movie
xyz.com/another-great-movie
当然,您也可以使用您选择的路由库完成所有操作。