HTACCESS: *.subdomain.domain.tld redir to subdomain.domain.tld/* - .htaccess

sorry for the vague question title.
But anyways,
I have here a subdomain which i wish to pass on wildcard sub-subdomains and make a proper htaccess redirect to a sub-folder (relative to the server root) equivalent to the wildcard value such that
*.subdomain.domain.tld will redirect to subdomain.domain.tld/*
where * = wildcard value
I hope you get my question. Can someone shed some light on this? I would appreciate it very much =)

RewriteRule ^(.*).subdomain.domain.tld$ http://subdomain.domain.tld/$1 [R]
That should do the trick.
The [R] tells the server that this is a redirect.
You can also use optional codes, say
[R=301] # Moved permanently
[R=302] # Moved temporarily
[R=403] # Forbidden
[R=404] # Not Found
[R=410] # Gone

You have to read the subdomain's name with RewriteCond and then you can do the Redirect with RewriteRule, while the subdomain is saved in %1.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.subdomain\.domain\.tld$ [NC]
RewriteRule .* http://subdomain.domain.tld/%1 [L,R=301]
If you want to redirect *.subdomain.domain.tld/file.html to subdomain.domain.tld/*/file.html, you can replace the RewriteRule with this:
RewriteRule ^(.*)$ http://subdomain.domain.tld/%1/$1 [L,R=301]

Related

How to redirect with .htaccess?

I have the following htaccess code already.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ index.php?page=$1
I would like that the htaccess redirect to
oszoczki.atwebpages.com/blog
when I type only
oszoczki.atwebpages.com
RewriteEngine On
RewriteRule ^$ /blog [R=301,L]
The ^$ in RewriteRule matches an empty URI and redirects to /blog.
R=301 represents a permanent direction.
L represents the last rule to be applied to this and ignore all others below it.
Update:
You can remove the L flag since redirection would have happened anyway as mentioned by #arkascha in the comments.

mod_rewrite seems to ignore a rule with a question mark?

I am trying to redirect
domain.com/page?user=something
to
domain.com/page/something
with
RewriteRule ^page?user=(\d[^/]+) /page/$1/ [R=301,L]
For some reason that's not working though. It seems to be just ignoring it even. When I go to domain.com/page?user=something nothing happens. mod_rewrite is enabled and all other rules are executing. I assume it could be due to the ? in the URL but I might be wrong?
You can use these 2 rules in your root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /page(?:\.php)\?user=([^\s&]+) [NC]
RewriteRule ^ /page/%1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^page/([^/.]+)/?$ page?user=$1 [L,QSA,NC]

Change displayed URL using htaccess

How could I rewrite my URL using HTACCESS?
When a visitor visits index.php, I want the URL to be rewritten to something else, although the visitor will remain on the index.php page.
This is what I've tried (I did research this before asking but couldn't solve it myself):
RewriteEngine on
RewriteRule ^index.php$ testingit.php
Basically, I just wanted to change index.php to 'testingit.php', just to see if it would work.
You can use this code in your testwebsite/.htaccess file:
RewriteEngine On
RewriteBase /testwebsite/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php[?\s] [NC]
RewriteRule ^ home [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteRule ^home/?$ index.php [L,NC]
So here's how you do it:
redirect *** /index.php http://www.yoursite.com/testingit.php
You need to replace *** with one of the following:
301-For a permanent redirect meaning browsers update bookmarks etc.
or
302-For a temporary redirect
Here's a link to a guide I found:
https://www.branded3.com/blog/htaccess-mod_rewrite-ultimate-guide/
Hope this helps :)
To make pretty URL's:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^home/?$ index.php [NC,L]

How can I redirect old pages with question marks in the URL?

I have read about htaccess redirect and rewrite on stackoverflow and on other sites and learned how to redirect simple pages and directories, but there are about 30 links remaining that I haven't been able to redirect. The reason appears to be because they contain "?" in the link's URL. I've tried the solutions posted but I haven't been able to make enough sense of them to succeed.
These work:
Redirect /Corpfleet.php htp://www.marketyourcar.cm/wraps.php
Redirect /drivers.php htp://www.marketyourcar.cm/drivers.php
Redirect /galleries.php htp://www.marketyourcar.cm/galleries.php
These do NOT work:
Redirect /ad.php?View=FAQ htp://www.marketyourcar.cm/advertiser-faqs.php
Redirect /ad.php?View=gallery htp://www.marketyourcar.cm/galleries.php
Redirect /ad.php?View=Materials htp://www.marketyourcar.cm/products-services.php
Yes, I know that the URL above is htp and .cm - I had to break it in order to make this post with my low reputation level.
If anyone can help with this I'd appreciate it.
Thanks
If you want to redirect from like:
site.com/index.php?blabla=1&id=32
to
site.com/contact.html
then use:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{QUERY_STRING} blabla=1&id=32
RewriteRule ^(.*)$ http://www.site.com/contact.html? [R=301,L]
</IfModule>
Redirect can't handle that. RewriteRule can. This should work.
RewriteEngine on
RewriteRule ^/ad\.php\?View\=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad\.php\?View\=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad\.php\?View\=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
Or try this:
RewriteEngine on
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php [R=301,L]
RewriteRule ^/ad.php?View=gallery$ http://www.marketyourcar.cm/galleries.php [R=301,L]
RewriteRule ^/ad.php?View=Materials$ http://www.marketyourcar.cm/products-services.php [R=301,L]
This might work:
Example:
RewriteEngine On
RewriteRule ^/ad.php?View=FAQ$ http://www.marketyourcar.cm/advertiser-faqs.php? [R=301,L]
Add a trailing ? to the substitution URL to remove the incoming query.

.htaccess Redirections

I've been Googling around for .htaccess redirection information, but nothing I find is quite what I'm looking for.
Basically, I want a solution that will take a site example.com and allow you to enter URL's like:
123.example.com
ksdfkjds.example.com
dsf38jif348.example.com
and this would redirect them to:
example.com/123
example.com/ksdfkjds
example.com/dsf38jif348
So basically accept any subdomain and automatically redirect to a folder on the root of the domain with the name of that subdomain.
Try something like this:
# If we're not on http://example.com
RewriteCond %{HTTP_HOST} .+\.example.com
# Add the host to the front of the URL and chain with the next rule
RewriteRule ^(.*)$ ${HOST}$1 [C,QSA]
# Make the host a directory
RewriteRule ^(.*)\.example\.com(.*)$ http://example.com/$1$2 [QSA]
You don't say what should happen to http://foo.example.com/bar?moo - I've made it go to http://example.com/foo/bar?moo
Change the last line if that's not what you want.
If you just want them to be the entrance:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ http://example.com/%1 [L,R]
Otherwise:
RewriteCond %{HTTP_HOST} ^([^.]+)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [L]

Resources