6

I recently with the help of SO solved my htaccess rewriterule issue:

Reuse subdomain in a rewrite rule

The result is that when somebody enters

whatever.example.com/anypage

in the adress bar, the htaccess automatically redirects to

whatever.example.com/somepath/whatever/anypage

What I wish to do is to find a way to just show whatever.example.com/anypage in the adress bar with the content of whatever.example.com/somepath/whatever/anypage displayed.

In the post I mentioned earlier, Jon Lin clearly mentions the following:

redirects always change what's in the browser's URL address bar

However I know some very frequent cases of url rewritting that would show in the adress bar let's say, for instance:

example.com/article-1-15

but actually showing the content of

example.com/somepath/somepage.php?article=1&otherparam=15

How could this apply to my case? I really wish to have a tiny url but it seems I missed something.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Sebas
  • 21,192
  • 9
  • 55
  • 109

1 Answers1

2

You may try something like this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI}  ^/([^/]+)$
RewriteRule .*  http://whatever.example.com/somepath/whatever/%1 [L]

It will map:

http://whatever.example.com/anypage

To a resource at:

http://whatever.example.com/somepath/whatever/anypage

showing always in the browser's address bar:

http://whatever.example.com/anypage

UPDATED

If whatever is dynamic and is the same in the substitution URI, here is another option:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST}  ([^/]+)\.example\.com.*
RewriteCond %{REQUEST_URI}  ^/([^/]+)$
RewriteRule .*  http://%1.example.com/somepath/%1/%2 [L]

This will work as long as both "whatever" in the substitution path are the same. If they are not, the last "whatever" has to be hardcoded, like this:

RewriteRule .*  http://%1.example.com/somepath/whatever/%2 [L]

There is no other way as the incoming URL doesn't have it.

Felipe Alameda A
  • 11,791
  • 3
  • 29
  • 37
  • "whatever" is dynamic, are you sure your htaccess would do it? – Sebas Dec 28 '12 at 23:12
  • Well, the only way to be sure it does what you need is testing it. – Felipe Alameda A Dec 29 '12 at 00:11
  • can't you see your htaccess will never handle dynamic subdomains? Maybe I am mistaken but it seems pretty obvious since you're hardcoding it in the rewriterule – Sebas Dec 29 '12 at 00:12
  • What the rule is trying to do is to keep the last folder `/anypage` as last folder. That's all, so the names of the folders or subdomains are not important. – Felipe Alameda A Dec 29 '12 at 00:19
  • @Sebas I understand what you said in your comment, so I updated my answer. I did not see it that way from your question. – Felipe Alameda A Dec 29 '12 at 00:49
  • the %1 is replaced by index.php (i'm asking for si.php page, so it must be a default directoryindex from my php.ini) and the subdomain name disappears from url. No trace of "si.php" either, that should go at the end instead of %2 if I understood well your rewriterule. – Sebas Dec 29 '12 at 01:04
  • The `%1` is the "whatever" subdomain in the requested URI `http://whatever.example.com/anypage` and the `%2` is the "anypage" folder in the same URI. – Felipe Alameda A Dec 29 '12 at 01:12
  • `http://testacc.example.com/si.php` redirects to `http://index.php.example.com/path/index.php/` – Sebas Dec 29 '12 at 01:17
  • There is no index.php in the rules in my answer, so it can't be redirecting to the URI in your comment because of my rules. I tested it and got: `http://testacc.example.com/si.php` is redirected to `http://testacc.example.com/somepath/testacc/si.php`, which is what you describe in your question. – Felipe Alameda A Dec 29 '12 at 01:23
  • damnit, my server must have something preventing it to run – Sebas Dec 29 '12 at 01:44
  • You can always test it [here](http://htaccess.madewithlove.be/), but maybe is the script si.php that is redirecting to `http://index.php.example.com/path/index.php/`. – Felipe Alameda A Dec 29 '12 at 01:56
  • Just in case, it is a silent redirection as you asked, so the substitution URI doesn't show in the browser's address bar. To show it, `R=301` has to be added to the flag **L** in the rule. – Felipe Alameda A Dec 29 '12 at 02:07
  • Suppose I Have URL Like www.admin.com/23445.zip which will automatically redirect to www.admin.com/files/23445.zip where 23445 will be dynamically ..what will be the rule then? – Hola May 11 '17 at 05:48