我决定为我的产品创建干净的URL链接以用于搜索引擎优化目的,因此客户可以更好地记住链接。我已经研究了几个小时,但没有完全理解干净和脏的URL重定向概念。您会认为有关此主题的文档会更简单。
我的.htaccess文件配置如下,并且非常适合使用用户输入的干净网址或干净的链接,例如www.example.com \ Category \ Subcategory \并将其重定向到.php文件以访问MySQL表并显示产品信息。
我无法弄清楚的是如何在用户的浏览器中显示干净的网址而不显示脏网或重定向的网址?这是在.htaccess文件中完成的吗?
更清楚:
如果客户输入:www.example.com \ shoes \ running,.htaccess将干净的网址重定向到subcat_lookup_clean.php?c = $ 1& s = $ 2,它基本上处理干净的网址并查找相应的信息,动态显示它。
但现在客户在浏览器中看到www.example.com \ subcat_lookup_clean.php?c = $ 1& s = $ 2.
相反,我希望他们看到干净的网址www.example.com \ shoes \ running。
.htaccess文件包含:
Options +FollowSymLinks
RewriteEngine On
# do not do anything for already existing file, symbolic link or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# redirect category/subcategory to lookup page
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ ./subcat_lookup_clean.php?c=$1&s=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ./subcat_lookup_clean.php?c=$1&s=$2 [L]
答案 0 :(得分:1)
在下面修正了.htaccess,解决方案运行正常。
您可以在此处演示:http://www.tiedandtrue.com
Options +FollowSymLinks
RewriteEngine On
# if redirected to url cleaner, redirect to actual lookup file and
# these lines will keep the clean url visible in the browser
RewriteRule ^cat_url_cleaner.php ./cat_lookup.php [L]
RewriteRule ^subcat_url_cleaner.php ./subcat_lookup.php [L]
RewriteRule ^subsubcat_url_cleaner.php ./subsubcat_lookup.php [L]
# do not do anything if already existing file, symbolic link or directory
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# redirect clean url (/category) for processing
RewriteRule ^([a-zA-Z0-9_-]+)$ ./cat_url_cleaner.php?c=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ ./cat_url_cleaner.php?c=$1 [L]
# redirect clean url (/category/subcategory) for processing
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ ./subcat_url_cleaner.php?c=$1&s=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ./subcat_url_cleaner.php?c=$1&s=$2 [L]
# redirect clean url (/category/subcategory/subsubcategory) for processing
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)$ ./subsubcat_url_cleaner.php?c=$1&s=$2&ss=$3
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/$ ./subsubcat_url_cleaner.php?c=$1&s=$2&ss=$3 [L]