Simple mod rewrite with no duplicate urls. It is possible?

天大地大妈咪最大 提交于 2019-12-12 04:23:14

问题


I have created simple mod rewrite

# REMOVE WWW
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^(.*)$ http://mydomain.com/$1 [R=301,L]


# URL REWRITE
RewriteEngine  on

RewriteRule ^design/?$ website-designing.php [L]

Everything working fine. My question is at present both URLs are working. How to prevent this?

mydomain.com/website-designing.php (my original URL)

mydomain.com/design/ (my SEO URL)

I want only SEO URL, same concept applied my all files in this directory (more than 150 URLs inside this directory)

It is possible?


回答1:


For each RewriteRule ^design/?$ website-designing.php [L] you could forbid access to corresponding resource.

RewriteRule website-designing.php - [F]

or maybe

RewriteRule .*\.php - [F]



回答2:


I think you'll have trouble to forbid access to the real .php file, since the client should be able to display it.

If you use clean URLs, I think the search engine can't find the file names if you do not register them in your code and forbid access to directories.

By the way your search engine friendly rewrite should look like :

RewriteRule ^design/$ /website-designing.php [L]

(Any one trying to access design/ got served /website-designing.php : no redirect so you won't see the name in the address bar).

I never used the nosubreq [NS] flag. It is said it filter internal subrequest generated by modules. But not sure it works with the internal HTTP calls. (Since you rewrite rule trigger a new request, all rules will be reprocessed so you can't just denied access to all .php files)

Maybe you can try :

RewriteRule ^.*\.php$ - [F,NS]

But do not expect many of this.

I'll try some tricks to see if something works.

Edit:

Seems that we can do something with : REDIRECT_URL or REQUEST_URI seems it seems they are conserved with rewritting. Like :

RewriteCond %{REDIRECT_URL} \.php
RewriteRule ^.*$ - [F]

RewriteRule ^design/$ /website-designing.php [L]

This should forbid any access when you call a .php file in the address bar.



来源:https://stackoverflow.com/questions/5700031/simple-mod-rewrite-with-no-duplicate-urls-it-is-possible

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!