Apache rewrite "domain.com/folder" to "folder.domain.com" with keeping redirection from "folder.domain.com" to "domain.com/folder" - apache

I want folder.domain.com to point at domain.com/folder
and
I want domain.com/folder to be rewritten in the URL of page in the browser to folder.domain.com.
In both cases I want to see an URL like this folder.domain.com.
Tried:
Redirect 301 /subdomain http://subdomain.example.com
Neither work:
RewriteCond %{THE_REQUEST} ^GET\ /ucp/profile\.php?([^=]+)=(\S+) [NC]
RewriteRule ^ucp/profile\.php$ /ucp/%1/%2? [R=301,L,NC]
# Now, deal with internal rewrites (which will not cause redirection):
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ucp/([^/]+)/([^/]+)/?$ /ucp/profile.php?$1=$2 [NC,L]
enter code here
And many many more.
Is it even achievable?

Related

htaccess error on redirect - too many redirects

I'm trying to redirect all requests from one domain (domain.co.in) to another domain (domain.info.in). I've tried Rewrite directives and Redirect directive in htaccess, but getting too many redirects error in browser. Below is the configuration I'm trying to implement.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.in [NC]
RewriteRule ^(.*)$ index.php/$1
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
My actual working htaccess configuration is
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1
Using this, I'm able to redirect all requests to index.php and working fine. But when I add domain rewrite rules, I'm getting the redirect error. I've tried Redirect directive also,
Redirect 302 / http://domain.info.in/index.php/$1, but same error.
I tried the Fiddler tool mentioned in this post, Tips for debugging .htaccess rewrite rules. There it is working good.
Actually, I want to redirect all requests (www.domain.co.in, domain.co.in, www.domain.info.in) to domain.info.in
Any suggestions on this?
As per #CBroe's suggestion, I've updated the configuration and it works. As he said,
RewriteConds always affect the directly following rule.
And I've also added negation to the checking to redirect all other requests.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^domain.co.in$ [NC]
RewriteRule ^(.*)$ http://domain.info.in/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php/$1 [L]

Apache redirect username to profile page conflict

I am trying to write redirect rules which will redirect usernames to profiles pages. The problem is I don't want to use extra parameter to determine that current url is for user. If I use example.com/user/test pattern then there is no problem. But I want to use example.com/test pattern.
The problem is also other pages uses this pattern beside user profile. For ex. example.com/messages. But these other pages are only a few. Hard coded exceptions should be fine on redirect rules.
Here is what I use for redirecting.
RewriteRule ^([^/]*)/$ /index.php?page=profile&username=$1 [L]
But for urls like /messages/, /login/, /settings/ I want to use this rule.
RewriteRule ^([^/]*)/$ /index.php?page=$1 [L]
Another problem when I remove ending slash from first redirect rule
RewriteRule ^([^/]*)$ /index.php?page=profile&username=$1 [L]
Like this, redirect loop happens. Actually I don't want to have ending slash but I fixed temporarily by adding it.
I am not sure if this affects other redirect rules but also I want to redirect index.php to ``.
To sum everything up I am listing what I am trying to do.
example.com/index.php => example.com
example.com/index.php?page=messages => example.com/messages
example.com/index.php?page=options => example.com/options
example.com/index.php?page=profile&username=test => example.com/test
Thanks.
Found it myself!
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/404
RewriteCond %{REQUEST_URI} !^/messages
RewriteCond %{REQUEST_URI} !^/login
RewriteCond %{REQUEST_URI} !^/logout
RewriteCond %{REQUEST_URI} !^/options
RewriteCond %{REQUEST_URI} !^/deposit
RewriteCond %{REQUEST_URI} !^/profile
RewriteRule ^(.*)$ index.php?page=view-profile&username=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?page=$1 [QSA,L]

How to block URLs different than main domain with Apache?

I want to block all request (or return 404 apaches error page) in my Wordpress different than the main url.
Example:
https://myurl.com this must work
https://myurl.com/fksdofiewoq this musnt work
https://myurl.com/wp-admin must work
myurl.com/mainpost must work clicking in the post in the main page.
Is there any posibility?
I could solve it with this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/index.php
RewriteCond %{REQUEST_URI} !/wp-admin
RewriteCond %{REQUEST_URI} !/wp-login.php
RewriteCond %{REQUEST_URI} !/hello-world/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !\.(pdf|jpeg|css|gif|ico|jpg|js|png|swf|txt|svg|woff|ttf|eot)$
RewriteRule . / [R=404,L]
This can filter that uris and block the rest of urls

friendly url only opens the post.php not cat.php

Friends, what is the right way to get both the php files to open friendly URL contents?
My current code works OK if I only use:
Options +FollowSymLinks
RewriteEngine On
# SEO URL Settings
RewriteBase /site/
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA,L]
but then I also need friendly URL for my categories so I tried to add:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]
but doing so only the post URL opens but category links redirect back to index.php but if you remove the rewrite for post than the cat.php contents shows.
If someone could help me out here would really appreciate your kindness.
This is because of your use of the [L] flag.
The [L] flag causes mod_rewrite to stop processing the rule set. In
most contexts, this means that if the rule matches, no further rules
will be processed. This corresponds to the last command in Perl, or
the break command in C. Use this flag to indicate that the current
rule should be applied immediately without considering further rules.
Documentation
Instead, try and have your rules laid out like this:
RewriteCond %{REQUEST_FILENAME} !-d [NC]
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteRule ^(.*)$ post.php?id=$1 [QSA]
RewriteRule ^(.*)$ cat.php?cat=$1 [QSA,L]

htaccess rewrite after redirect

I have a site which uses htaccess to rewrite all pages to the index page with a hash which is then used to serve up content. The file looks like this....
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
I am now moving some of the pages of the site, however if I add a redirect such as....
Redirect 301 /blog /new_location/blog/
I am running into problems with the resulting url looking like
https://mydomain/new_location/blog/urlpath=blog.php
Can anyone suggest a way that I get the page to redirect to mydomain/new_location/blog/ and then run the rewrite on the new url.
Many thanks
RewriteRule and Redirect are from different Apache modules, so run at different times in the processing, not in the order they appear in the configuration. You're best off sticking to one module or the other, by using the [R] flag to RewriteRule.
RewriteRule /blog(.*) /new_location/blog$1 [R=301]
OK, I managed to get this working using a combination of Redirect and Rewrite like so....
Redirect 301 /blog /new_location/blog
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/blog
RewriteRule ^([^?]*)$ /index.php?urlpath=$1 [NC,L,QSA]
Maybe not the neatest solution, but it work!