I need to create an htaccess rule for a website i built in codeigniter for an apache web server.
I basically have a one page website which displays sections of the site on the the homepage.
for example...
http://www.example.com/sales
http://www.example.com/portfolio
...all appear as sections on the homepage so it display on one page only
My plan is to use anchor tags and hashes to direct the user to the right area on the homepage but I can't figure out the correct htaccess rule to modify the url to redirect to the homepage and to the correct anchor point
So my prefered method to do this would be to a rule to redirects to the homepage like so...
**http://www.example.com/sales**
redirects to
**http://www.example.com/#sales**
And if the url has more directories I need them to go to the highest parent on the home page
for example...
**http://www.example.com/sales/future/page1**
redirects to
**http://www.example.com/#sales**
I would also need to make sure that i hide the index.php that displays at the beginning of the segment!
Any ideas or alternative suggestion to get this working would be greatly appriciated
Kind Regards
You can use this code in your DOCUMENT_ROOT/.htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)(/|$) /#$1 [L,NE,R=302]
Related
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.
I've a backlink of my website as
http://localhost/babycare/article/detail/93#591
I want to redirect it to a new user-friendly url like
http://localhost/babycare/article/detail/feeding_aversion
The code i've written in my .htaccess file is here
RewriteCond %{THE_REQUEST} /article/detail/93 [NC]
RewriteRule ^/?(babycare)? /article/detail/Feeding_Aversion? [R=301]
But it redirects to
http://localhost/babycare/article/detail/feeding_aversion#591
How could i remove #591 from url.
The problem you've got is that location hashes usually only exist in the browser, and are not meant to be handled by the server.
Its covering in more detail in the following answers;
URL Fragment and 302 redirects
Which one is better pushstate or location.hash?
You could use Javascript to remove the hash's if they're that much of an issue.
location.hash = "";
Should achieve what you're looking for - obviously it'll need to go onto each page.
As of now, my htaccess looks like this
RewriteEngine On
RewriteCond %{QUERY_STRING} ArticleID=([^&]+)
RewriteRule ^article.cfm$ /articles.php?id=%1 [R=301,L]
RewriteRule ^([a-zA-Z0-9-]+)$ page.php?slug=$1
RewriteRule ^([a-zA-Z0-9-]+)/$ page.php?slug=$1
ErrorDocument 404 "<H1>Page not found</H1>"
The first 3 lines allow me to redirect users who are access articles through .cfm to .php instead
For the 4th and 5th lines, my website allows creation of pages through a WYSIWYG editor and allow users to access it. The actual url will look like www.website.com/page.php?slug=homepage but after the rewrite rule, it will look like www.website.com/homepage
However, I was having problems where my admin pages such as www.website.com/admin/ was regarded as one of the page so I added the below codes and it works
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
But I'm still having a couple of problems.
If you access an invalid page such as www.website.com/hello/ when there is no such page created by the users at all, it will still show page.php but with empty contents, how do i redirect it to a 404 page?
How do I secure directories which does not have an index.php page? such as my images folder, includes folder, javascript folders etc?
Does my entire htaccess looks right at this current point?
Thank you guys so much for helping! I'm not good with mod_rewrite so I will appreciate your help!
If you access an invalid page such as www.website.com/hello/ when
there is no such page created by the users at all, it will still show
page.php but with empty contents, how do i redirect it to a 404 page?
You should do this with PHP. If there's no content included, use header() to redirect to a 404.
How do I secure directories which does not have an index.php page?
such as my images folder, includes folder, javascript folders etc?
Add this to your .htaccess: Options -Indexes
Does my entire htaccess looks right at this current point?
Looks fine.
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]
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]