Apache Rewrite - redirecting a substitution subdomain and processing an internal URL

I have a problem with redirecting the wild-card subdomain and shortening the internal URL.

Say I have an internal URL in my application

example.com/b/ABCDE

which will translate

example.com/book/12345678-the-book-name

The script referenced /b/(I use the PHP framework that can handle the URL rule) will translate the short identifier ABCDEinto the real identifier of the book 12345678(and the title "Book Name"), and then redirect this to the permanent URL of the bookexample.com/book/12345678-the-book-name

so every time I distribute a link to a book on sites such as message boards, micro-blog sites, or physical media like posters or business cards, I use a short link ( example.com/b/ABCDE) instead of a permanent link ( example.com/book/12345678-the-book-name).

next, I need to redirect all wild-card subdomains to the main domain ( www.example.com) while saving the request URI, for example.

http://random.example.com/book/11111111-some-book -> http://www.example.com/book/11111111-some-book
http://123456.example.com/book/22222222-another-book -> http://www.example.com/book/22222222-another-book
http://abcdefg.example.com/book/33333333-another-book-again -> http://www.example.com/book/33333333-another-book-again

adding the rule below after all the rules that I used

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RewriteEngine on

RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>

Therefore, a url with the domain example.com and without a prefix, as shown below

http://example.com/book/11111111-some-book

will switch to

http://www.example.com/book/11111111-some-book

And another consequence is that if the internal URL shortener uses a simple domain without a prefix, two redirects will be required to eliminate it. For instance,

http://example.com/b/ABCDE

will be redirected to

http://www.example.com/b/ABCDE

which is then redirected to

http://www.example.com/book/12345678-the-book-name

, . SEO , . ( )

,

<VirtualHost *:80>
ServerName example.com
ServerAlias *.example.com
RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteCond %{REQUEST_URI} ^/b/(.*)$ 
RewriteRule . index.php [L]

RewriteCond %{HTTP_HOST} !^www.example.com [NC]
RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>

Apache, http://htaccess.madewithlove.be/, . , 400 Bad Request example.com/p/ABCDE.

, :

  • SEO ? - , -?
  • 400 Bad Request?
  • ? URL (example.com/b/ABCDE www), .
+5
1

SEO?

SEO, , , , .

, , . , . "" SEO ( URL-, ).

400

RewriteRule,


^/somepath(.*) --> otherpath$1: ,
^/somepath(.*) --> /otherpath$1: /otherpath/pathinfo

, vhost conf (, Apache, , ). 400:

RewriteRule . /index.php [L]

, index.php ,

ServerName www.example.com
UseCanonicalName On

$_SERVER["SERVER_NAME"] www.example.com URL-, .

conf

<VirtualHost *:80>
    ServerName www.example.com
    ServerAlias *.example.com example.com

    UseCanonicalName On
    RewriteEngine on

    #set the document root
    DocumentRoot /path/to/the/app 

    # if something goes wrong, setup logs to track what happens
    # comment these lines when you're done
    ErrorLog /a/path/to/a/log/file

    RewriteLogLevel 5
    RewriteLog /a/path/to/another/log/file

    # I simplified the conditions, those are equivalent to your rules
    # a RewriteRule tries to match against %{REQUEST_URI}
    RewriteCond %{HTTP_HOST} ^example\.com [NC]
    RewriteRule ^/b/ /index.php [L] 

    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
    RewriteRule ^/(.*)$ http://www.example.com/$1 [R=301]
</VirtualHost>
+3

All Articles