How to add a custom word to my domain URL for every request?

非 Y 不嫁゛ 提交于 2019-12-06 14:40:56

问题


Consider my domain name is

www.mydomain.com

Consider a page request

www.mydomain.com/user/register

I want to add a custom word after base URL for every request inside mydomain.com.example

www.mydomain.com/customword/
www.mydomain.com/customword/user/register

Can we do this using URL rewriting in htaccess file ?

Actually it should execute 'www.mydomain.com/user/register' internally...but externally the URL should look like www.mydomain.com/customword/user/register.


回答1:


You could create the directory "register", and put an index file inside it that performs the action.

That's probably the simplest way without url rewriting anyway.


UPDATE (since the question was updated)

In .htaccess:

RewriteEngine On
RewriteRule ^([A-Za-z0-9-.]+)/user/register/?$ user/register.php?customword=$1

register.php will receive a GET request:

//User went to www.mydomain/word/user/register
echo $_GET['customword']; // will return word in this case

Make sure that you have mod_rewrite enabled :)




回答2:


Yes, you can do it with htaccess

Here is an example which will add a trailing slash with url if it doesnt contain trailing slash

http://enarion.net/web/htaccess/trailing-slash/




回答3:


edit formatting updated
If you are serving one site from this then the following should work:

Edit your .htaccess file to do a url rewrite

accessing www.yourdomain.com/user/registry will actually server content from www.yourdomain.com/customword/user/registry

RewriteEngine On<br>
RewriteCond %{REQUEST_URI} !^/customword/<br>
RewriteRule ^(.*)$ /customword/$1

You haven't mentioned what kind of site you;re using..eg: PHP, MVC etc as you could do similar thing in there as well.



来源:https://stackoverflow.com/questions/12399483/how-to-add-a-custom-word-to-my-domain-url-for-every-request

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