I need to add redirects to .htaccess.
If the URL address does not contain /site/ or /builder/, redirect to /site/
Example:
http://www.mh.cz/test.php -> http://www.mh.cz/site/test.php
http://www.mh.cz/builder/a.php -> http://www.mh.cz/builder/a.php
http://www.mh.cz/site/contact.php -> http://www.mh.cz/site/contact.php
Actualy .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.mh.cz [NC]
RewriteRule ^(.*)$ http://mh.cz/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mh\.cz$ [NC]
RewriteCond %{REQUEST_URI} !/site/
RewriteRule ^(.*)$ http://mh.cz/site/$1 [L,R]
How to modify the condition to redirect be ignored if the URL already exists /builder/ ?
Thanks
Using .htaccess redirect all pages to index.php or whatever you want.
In index.php, grab the requested URL and make conditions to determine which page to include depending on the URL.
For example, http://www.example.com/test.php will display content of http://www.example.com/site/test.php but in the address bar http://www.example.com/test.php will remain, the user will not get Error 404.
I'm not sure if this will fulfill your actual need but it will work.
Related
I have...
| .htaccess : (v1)
RewriteEngine on
RewriteRule ^in?$ login.php
So, /in --is-really--> /login.php
This much works great. We all can learn how to do this from: .htaccess redirect with alias url
But, I want it to also work in reverse...
If someone should enter /login.php into the address bar, I want it to change to /in.
So also, /login.php --rewrites-to--> /in
From this Answer to a different Question, I want to be ready for anything, using REQUEST_URI. So, my .htaccess file starts with this...
| .htaccess : (v2)
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php
RewriteRule ^in?$ login.php
That also works great.
But now, I want to add this rule (my Question here) for /in <--> /login.php both ways, just how / <--> /index.php already works with .htaccess (v2). So, I adopted the settings and added a second rule...
| .htaccess : (v3) —not working!
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php
RewriteRule (^|/)index\.php(/|$) /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php
RewriteRule (^|/)login\.php(/|$) /%1in [R=302,L]
RewriteRule ^in?$ login.php
...but then /in and /login.php both cause an infinite redirect loop.
What's the right way to do this, still using REQUEST_URI, and still having both rewrite rules (for index.php and for login.php)?
These Questions did not help:
Rewrite rule to hide folder, doesn't work right without trailing slash
This is not about a trailing slash
Allow multiple IPs to access Wordpress Site Admin via .htaccess
This is not about IP-based access
Htaccess URLs redirects are working for http not all https
This is not about https vs http
Rewrite-rules issues : .htaccess
This is not about cleaning up the GET array in the URL
apache htaccess rewrite with alias
This is not about rewriting the host/domain, thereby preserving the path
rewrite htaccess causes infinite loop?
This is not about www subdomain rewrites
.htaccess rewrite page with alias
This is not about rewriting "pretty" URLs nor about how to use slug settings in WordPress
Htaccess alias or rewrite confusion
This is not about simply having multiple rules with the same destination
htaccess rewrite to include #!
I'm not trying to rewrite #!
Reason of redirect loop is a missing RewriteCond %{ENV:REDIRECT_STATUS} ^$ before first redirect rule that removes index.php. Remember that RewriteCond is applicable to immediate next RewriteRule only.
Suggested .htaccess:
RewriteEngine on
# Remove index.php, if a user adds it to the address
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?index\.php$ [NC]
RewriteRule ^ /%1 [R=301,L]
# "in" --> login.php, and also redirect back to it
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{REQUEST_URI} ^/(.+/)?login\.php$ [NC]
RewriteRule ^ /%1in [R=302,L]
RewriteRule ^in?$ login.php [L,NC]
It won't cause redirect loop because after first rewrite to /login.php, variable REDIRECT_STATUS will become 200 and then the RewriteCond %{ENV:REDIRECT_STATUS} ^$ will stop redirect looping.
Thanks to the help from the user with the correct answer, I found that...
RewriteCond %{ENV:REDIRECT_STATUS} ^$
...doesn't go in .htaccess only once, but every time on the line before...
RewriteCond %{REQUEST_URI} ...
At the moment, when I visit example.com/index.php?page_id=6726, the desired page is displayed. I would like to rewrite the URL to the format of example.com/newsletter.
My current .htaccess file includes this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^example\.com\/newsletter [NC]
RewriteRule ^(.*)$ index.php?page_id=6726 [NC,QSA]
Yet, I receive a page not found error.
How can I make the rewrite work so that when I visit example.com/newsletter the content of example.com/index.php?page_id=6726 is displayed?
RewriteEngine On
RewriteRule ^newsletter$ index.php?page_id=6272 [L,NC]
More generally, if the page_id is a variable, so
RewriteRule ^newsletter/?(\d+)?$ index.php?page_id=$1 [L,NC]
will redirect the visitor to many similar pages, such as
example.com/newsletter/6272 -> example.com/index.php?page_id=6272
example.com/newsletter/272 -> example.com/index.php?page_id=272
example.com/newsletter/627 -> example.com/index.php?page_id=627
...
I have the following .htaccess code:
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^$ subpage [L]
So basically if someone visit "www.example.com," he'll see the content of "www.example.com/subpage" without the url changed. This is good.
However, they can still visit the page by "www.example.com/subpage." If that happens, I want the url changed back to "www.example.com."
Is it possible? What I've tried so far gave me redirection loop.
You need an additional rule that matches against the actual request and not the URI. Since the rewrite engine loops, the URI keeps changing, so you need to match against the %{THE_REQUEST} variable. You need this rule before the rule that you have in your question:
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} \ /+subpage(\?|\ |$)
RewriteRule ^ / [L,R=301]
Pretty specific htaccess question: I have a page at http://subdomain.example.com/page/ that contains a form. The page should only be accessed with auto-created links that contain query strings that feed information through the form. What I need is an htaccess rule that will redirect traffic coming to this page to my homepage at http://example.com/ whenever it is reached without a query string in the URL.
Put another way: if someone reaches http://subdomain.example.com/page/ they need to redirect to http://example.com. But if someone reaches http://subdomain.example.com/page/?querystring they need to stay on the page.
How do i solve this with htaccess?
try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^page/ http://example.com/ [L,R]
or
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.example\.com$ [NC]
RewriteCond %{THE_REQUEST} !\?
RewriteRule ^page/ http://example.com/ [L,R]
I'm trying to redirect two kind of urls to a subdomain and all the others to my main domain.
A concrete example :
"my account" pages starting by /my-account/* and subscription page must be redirected to https://my-account.domaim.com. The uri must be kept.
all others like /news or the homepage must be seen on www.domain.com only
Here is what I have tried until now :
# All urls except my-account/* and subscription are redirected to the main domain
RewriteCond %{HTTP_HOST} ^my-account\.domain\.(.+)$
RewriteCond %{REQUEST_URI} !^my-account/
RewriteCond %{REQUEST_URI} !^subscription$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,QSA,R=301]
# subscription page is redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# All my-account/* pages are redirected to my-account subdomain
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^my-account/(.*)$ https://my-account.domain.%1/my-account/$1 [L,QSA,R=301]
Each rules work independently but if i try all of them together i'm stuck in an infinite loop.
Is anyone have an idea how to prevent it ?
The rules look fine, but you have a missing space in your 2nd rule:
RewriteRule ^subscription$ https://my-account.domain.%1/subscription[L,QSA,R=301]
# -----------------------------------------------------------------^
But you can probably combine them into a single rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.(.+)$
RewriteRule ^(subscription|my-account/.*)$ https://my-account.domain.%1/$1 [L,QSA,R=301]
Also make sure you're clearing your cache when you test, as 301 redirects are permanent and the browser will cache them.
Thanks for your answer !
The typos was from my copy/paste and the combination works but doesn't change anything to the problem with the other rules. I keep it for later ;)
I tried a reversed rules like this :
#RewriteCond %{HTTP_HOST} !^my-account\.domain\.(.+)$ [NC]
#RewriteCond %{REQUEST_URI} ^/subscription$ [OR]
#RewriteCond %{REQUEST_URI} ^/my-account/ [NC]
#RewriteRule ^(.*)$ https://my-account.domain.%1/$1 [L,R=301]
It also works but not in combination with the other. it doesn't loop anymore but if i try something like http/www.domain.com/subscription i'm redirected to www.domain.com with the url truncated. It seems that the Rewrite conditions aren't correctly recognized but still can't find why ...