Using MVC. Is there a way to make any url like .example.com to point to example.com/user/
as in http://marwan.example.com to point to example.com/user/marwan
The controller is user and the action is index which takes marwan as its variable.
One idea I had is to make a custom error page to handle 404 and see if the url starts with something like *.example.com and redirect to the proper controller. There must be a better way to do this!
Take a look at this answer it may help you out.
ASP.NET URL Rewriting
You can try the following:
RewriteConf %{HTTP_HOST} !^example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/user/%1$1
RewriteRule ^/user/([a-zA-Z0-9]+)\.example\.com/(.*) http://example.com/user/$1/$2 [NC,L]
What this rule does is first insert the host into the URL path and then strip out the correct values from the host and write it the way you want on the backend.
Related
I want to know if there is a way to prevent visitors on the website to see the full site url .. so for example instead of http://www.5eren.dk/index.php all the user see's is http://www.5eren.dk .. is this possible ?
If i am understanding you correctly check out .htaccess rewrite condition and rewrite engine.
For example to hide a file extension:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
This should get you started.
Simply NO
However, depending on your server-side scripting technology you use on your website, such functionality may be applied by a mean of central script that call some URL on your website and render it as it is being the requested script with some param.
For example:
Suppose the following URL
http://myexample.com/secrets/pages/output.php
Now we may request it using some routing script as follows:
http://myexample.com/routing.php?routeID=123
Using some database inside your routing script you may able to call or include the secrets/pages/ouput.php
I would like to be able to access this URL on my website:
www.mysite.com/author/username
trough this one
www.mysite.com/username
Please consider that people should be able to access using the two url and that the "username" always change depending on the user.
What would be the best way to do it? htaccess rewrite?
Thank for your time!
Seems like it could be messy and box you in if your users create names that match paths that overlap with existing or future paths.
RewriteCond %{REQUEST_URI} ^/[a-z]+$ #1
RewriteCond %{REQUEST_FILENAME} !-f #2
RewriteRule ^/([a-z]+)$ /author/$1 [P] #3
try to restrict it as much as possible to your username pattern
only rewrite if the path doesn't already exist
proxy to the /author/username version of the URL
I have a short question about url rewriting...
I have a website. Let's say http://www.example.com/
with some subsites:
http://www.example.com/a.php
http://www.example.com/b.php
etc...
and some "special" urls like
http://www.example.com/c.php?i=1#link1
http://www.example.com/c.php?i=1#link2
http://www.example.com/c.php?i=2#link1
Now I would like to write a .htaccess file to convert current urls into rewritten urls like
http://www.example.com/a/
http://www.example.com/c/1/#link1
I'm not an expert in url rewriting, so could somebody please help me?
Best reagards.
RewriteRule ^a$ a.php [L]
RewriteRule ^c/(.*)/(.*) c.php?i=$1$2
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
RewriteRule ^a.php a/
RewriteRule ^b.php b/
Should work as far as your static pages go, you can not rewrite the hash because the hash is not sent to the server (as discussed here). If you need to rewrite the hash I would suggest changing the hash to another GET variable (eg u), in that case just add this to your htaccess: RewriteRule ^c.php?i=(.*)&u=(.*) c/$1/$2. If you simply intend to leave the anchor though, you can omit it from your rewrite and everything should be fine (...becuase the server never sees the hash/pound symbol), and in that case you should add this to your code RewriteRule ^c.php?i=(.*) c/$1/.
I have the following rewrite rule which is working fine:
RewriteRule ^([a-zA-Z\-]+)[/]?$ apply/?survey_folder=$1
which turns this: address.com/folder1/
into this: address.com/apply/?survey_folder=folder1
The problem is that I can't figure out a way to use a redirect to the rewritten URL. Using cfml as an example, (cflocation url="http://www.address.com/folder1/") throws an error because, of course, the folder "folder1" doesn't actually exist on the server.
I can get the user redirected to the correct page by using /apply/?survey_folder=folder1, but that defeats the purpose of having the rewrite rule at all.
Is there any way to redirect to a URL rewritten by htaccess? I'm new at RewriteRule.
Add the [L] flag to the rule otherwise you're in danger of infinite loops and other problems.
The redirect you need is incredibly simple.
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /apply/(index\.php)?\?survey_folder=([\ ]+)\ HTTP/
RewriteRule ^apply/(index\.php)? http://www.example.com/%1 [R=301,L]
RewriteRule ^([A-Z-]+)$ /apply/?survey_folder=$1 [NC,L]
Do not let both URLs with trailing slash and URLs without trailing slash resolve to the same content. The URL for a page does not have a trailing slash. Add another preceding redirect to strip trailing slashes in that case.
Have you tried setting RewriteBase? Its purpose is to map the URL to the physical location on the server. I'm not completely sure this is the problem you are having, but it does sound like a possibility.
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritebase
I need to grab some of my website's old URLs and do a 301 redirect to the new ones, since they are already indexed and we don't want to loose relevance after the change. The old URL is in fact very ugly and for some reason everything I try to do to rewrite it does not work. Here it is:
http://www.mywebsite.com/ExibeCurso.asp?Comando=TreinamentoGeral&codCurso=136&Titulo=Como%20Estruturar%20um%20Sistema%20Gerencial%20de%20Controles%20Organizacionais,13
Basically, I need to translate it into something like:
http://www.mywebsite.com/curso/136
From the old URL I need to check if the user typed "ExibeCurso.asp"; then I know I must send him here: /curso. I must also grab the integer that was in the querystring parameter "codCurso" (136). What is the regular expression I must use for this. I am using ISAPI_Rewrite 3, which basically implements htaccess on IIS, so there should be no difference in terms of syntax. Thanks.
Try this rule:
RewriteCond %{QUERY_STRING} ^([^&]*&)*codCurso=([0-9]+)(&.*)?$
RewriteRule ^/ExibeCurso\.asp$ /curso/%2? [L,R=301]
But I’m not sure whether ISAPI Rewrite requires the pattern to begin with a slash.
Off the top of my head, something like this should work:
RewriteRule ^ExibeCurso.asp(.*)$ http://www.mywebsite.com/curso/$1 [L,R=301]
That would at least send the traffic to /curso/ with all parameters attached. Maybe it's best to process it from there.