I’m having troubles linking to pages when using a wildcard subdomain and would love a little help figuring out a solution.
I want the users to have their own subdomain to view their own website content so if a user types in "user1.domain.com" the server would have a rewrite rule that directs the request to the index.php page in the root directory. This index.php page will be used for all subdomains and will serve the content out of a db based on the subdomain passed in the querystring.
This part is working great and it serves up the content from index.php as it should. When index.php page loads it includes a menu (HOME | RENTALS | CONTACT US). The problem comes when I click on the menu link to go to another page. The URL address changes, but the page content does not.
Here's an example: If I click on "RENTALS" I want the user to go to the rentals.php page and display the rentals available for the "user1" subdomain...but here's what is happening:
Link scenario 1:
RENTALS --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
Link scenario 2:
< a href="rentals.php" >RENTALS< /a > --> URL address bar changes to "user1.domain.com/rentals.php" but the page doesn't change, it still stays on the index.php content
Link scenario 3 (if I hardcode the domain and ommit the wildcard subdomain the link works):
RENTALS --> URL address bar changes to "http://www.domain.com/public_site/rentals.php" AND IT WORKS, the rentals.php page loads and serves up the content.
The problem with scenario 3 is I want the url to read: "user1.domain.com/rentals.php"
I'm wondering if this can be setup in a rewrite condition, but I'm not able to figure it out.
If anyone can help I would be grateful!
Here is my directory structure:
ROOT
|
PUBLIC_SITE
|
index.php
rentals.php
contactus.php
Here is the apache rewrite rule (the 1st rule works. the 2nd rule I added hoping to solve my problem, but it didn't solve the problem):
<IfModule mod_rewrite.c>
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
####Rewrite subdomain request to public_site/index.php
RewriteCond %{HTTP_HOST} !www.domain.com$ [NC]
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com [NC]
RewriteRule ^(.*)$ /public_site/index.php?subdomain=%2 [L]
#### Rewrite rule for rentals.php page
RewriteCond %{HTTP_HOST} ^(www.)?([a-z0-9-]+).domain.com/rentals.php [NC]
RewriteRule ^(.*)$ /public_site/rentals.php?s=%2 [L]
</IfModule>
figured it out on my own after getting into the rewrite...
simply put this code before the "main domain" rules:
#### Rewrite rule for rentals.php page
RewriteCond %{REQUEST_URI} ^/rentals.php?$ [NC]
RewriteRule ^(.*)$ /public_site/rentals.php [L]
Related
I am trying to create a multidomain solution with the following setup:
both domains subdomain1.domain.com and subdomain2.domain.com point to the same directory on the server
if one visits the URL https://subdomain1.domain.com/subdomain-category/subdomain-article he should be redirected to https://subdomain2.domain.com/subdomain-article
this cannot be solved with a simple redirect, as https://subdomain2.domain.com/subdomain-article does not exist
because subdomain1.domain.com and subdomain2.domain.com point to the same directory, https://subdomain1.domain.com/subdomain-category/subdomain-article is identical to https://subdomain2.domain.com/subdomain-category/subdomain-article, but it is needed to "mask" the second one in order to show only https://subdomain2.domain.com/subdomain-article - therefore not changing the URL on the second RewriteRule
So far I tried many different rules & combinations, getting closest to a solution with the following:
RewriteCond %{HTTP_HOST} ^subdomain1\.domain\.com$ [NC]
RewriteCond %{REQUEST_URI} ^/subdomain-category/(.*)$ [NC]
RewriteRule ^(.*)$ https://subdomain2.domain.com/subdomain-category/%1 [R=302,NC]
...which works so far.
Now, what is missing are the rules that show the content of the page https://subdomain2.domain.com/subdomain-category/subdomain-article while the URL in the address bar of the browser should read https://subdomain2.domain.com/subdomain-article.
Thanks in advance for any valuable hint/info!
I've just re-developed one of my websites. The structure has changed therefore I need to globally redirect the old non existent pages that are still currently ranked on Google (50 or so pages) to redirect to my new website's HOME PAGE.
The structure has basically changed from pages - example:
www.website.com/page1.php
www.website.com/page2.htm
www.website.com/page3.php
... and so forth
to
www.website.com/page1/
www.website.com/page2/
www.website.com/page3/
... and so forth
After a bit of Googling, I came across this solution on SO
When I add:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]
to my .htaccess file, the redirect (to the home page) for non-existing pages works - Great!
BUT now every link in my navigation / side panel on my new website (that exists) is also being redirected to the home page. So I cannot navigate through my website!
So for example when I click the about us link www.website.com/about-us/ or any other of my other 50 pages (regardless what they're named), the 301 redirect solution above keeps re-directing me to home page (as is if those pages didn't exist).
I want to globally redirect the old non existent pages that are still currently ranked on Google (50 or so pages) to redirect to my new website's HOME PAGE ... NOT my website's existing links.
Why is this? I thought the 404 redirect using 301 method would only target pages that DON'T exist.
Am I doing anything wrong?
To redirect /page.php to /page , you can use the following :
RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} !200
RewriteRule ^(.+)\.php$ /$1 [L,R,NE]
You can change R to R=301 to make the redirect permanent.
The rule is applied as follows
Check if about-us matches the pattern ., yes it does.
Check if the conditions match, %{REQUEST_FILENAME} !-f. It is a directory, so yes it is not a file.
Everything matches -> apply the rule.
If you don't want a directory to match, you may say so by
RewriteCond %{REQUEST_FILENAME} !-d
So the complete rewrite rule would be
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R]
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
Can't seem to find any way to do this:
I have ah .htaccess redirect set up on my site to take visitors from my homepage site.com to a landing page on welcome.site.com.
RewriteRule ^(index.html)?$ http://welcome.example.com [R,L]
However, the problem is that we also have users accesing and using the site thought a site.com/login url and now any time they click home on the navigation menu or any other links in there, they are taken to the landing page instead of the proper site.com page. Is there any way to set up the .htaccess in such a way so as to exclude any visits coming from inside the domain from this redirect?
You could check the %{HTTP_REFERER} parameter and see if it contains your domain name:
RewriteCond %{HTTP_REFERER} !(www\.)?example.com$ [NC]
RewriteRule ^(index.html)?$ http://welcome.example.com [R,L]
About the project: customers are able to book/book not subdomains for their content...
So if you book the subdomain option, you have your content appearing on sub.example.com and if you don't book this option, the content appears on example.com/sub like content appears regularly. I'm trying to achieve this with the following redirection mechanism.
Somebody visits sub.example.com
The browser shows the content of example.com/sub
The address bar still shows sub.example.com
Achieved with following .htaccess located under sub.example.com
RewriteEngine on
RewriteCond %{REQUEST_URI} / [NC]
RewriteRule ^(.*)$ http://example.com/sub [P]
All the links on example.com/sub or sub.example.com are absolute links, so if I click on the main logo on sub.example.com, I get redirected to example.com and the browser bar changes. If I click on an article appearing on sub.example.com, it contains an a href to example.com/article so I reach example.com/article just fine.
My questions are these:
Is the .htaccess okay?
Are there any problems because of duplicate content? example.com/sub and sub.example.com look the same from the perspective of search machines. How could I manage the duplicate content problem?
Is this approach clever in general? If not, how am I able to achieve my requirements?
Are they all on the same server? The P-flag is generally used when the destination is not on the same server. Otherwise, just rewrite your page (see below). The address bar stays the same, but the content will be redirected.
Duplicate content can be solved by adding a meta tag (canonical) to the rewritten pages
=> https://support.google.com/webmasters/answer/139066?hl=en
If they are on the same server: No !
Example code:
RewriteEngine on
RewriteCond %{REQUEST_URI} !/sub [NC]
RewriteCond %{HTTP_HOST} ^sub\.example\.com [NC]
RewriteRule ^(.*)$ /sub/$1 [QSA,L]
I have to make a maintenance page for my website with the .htaccess, I've searched on the internet and could only find snippets/scripts which redirect but I only want that it displays a message on the page itself for people, but not for a specified ip-address. So when I enable the script in the .htaccess it has to show a message on every page/ file(=css, etc.) except for my ip-address
This should work.
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REMOTE_ADDR} !=123.45.67.89
RewriteCond %{REQUEST_URI} !^/construction.php
RewriteRule ^ /construction.php [L]