I recently put up a site and I have been doing some SEO.
However I noticed that links from Google search append index.php to my links.
For example a site page which clearly appears as www.example.com/index/why on search together with correct content sample when clicked on ends up in the new browser as www.example.com/index.php/why
Note that on my site all links are redirected to SSL and I use the MVC stucture.
Any directives that am may be missing?
My .htaccess file is as below
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
#RewriteCond %{QUERY_STRING} !vardump
This might be some kind of URL rewrite / redirection issue at your site. Try viewing the net requests while clicking on the incoming link in Firebug to make sure there is no weird redirection at your own website.
Edit: The second last line in your htaccess file causes the unwanted redirection you are describing.
I found a solution to my problem and I decided to write the .htaccess rule/condition that saved the day.
RewriteCond %{HTTPS} !^on$
RewriteRule ^(index).php(/.*) https://%{HTTP_HOST}/$1$2 [R,L]
Hope this helps someone.
Related
I am trying to make a rewrite rule so if a visitor types:
https://mywebsite.com/app/invite/abc123
it gets parsed as:
https://mywebsite/invite.php?id=abc123
using the following code:
RewriteRule ^app/invite/(.+)$ /app/invite.php?id=$1
The issue is that all relative links break after rewriting, as the invite is not a real existing directory and it is added to all relative links.
How can I fix this or prevent adding the invite directory to links?
I wanted to point out that I use the following rules in my .htaccess file.
RewriteEngine On
# Redirect http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect www to non-www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# Remove php file extension from links
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
I am trying to adjust all they do not result in infinite redirects or interval server errors.
I use Apache/2.4.46.
I finally managed to solve this issue by using this code:
RewriteRule "^app/invite/([0-9a-zA-Z]+)" "/app/invite?id=$1" [NC,L,QSA]
and in the invite.php I set the base in the <head> tag to:
<base href="../">
So it jump back to the actual directory and don't break the website links.
If any one has a better approach, I will be delighted if they share with me.
Cheers!
So I have successfully deployed a Flask app using CGI. In order to get the app working, my .htaccess file had to look like this:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /home/user/public_html/mysite/cgi-bin/main.cgi/$1 [L]
However, I am facing a couple of issues.
mysite.com works, but when I link other pages to the home page, it takes them to mysite.com/cgi-bin/main.cgi. I'd like for the links to take users to mysite.com. Similarly, when I try to link to another page, it goes to mysite.com/cgi-bin/main.cgi/page2, when I actually want it to be mysite.com/page2. How can I fix this?
The following .htaccess content seems to not work:
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
Header always set Content-Security-Policy "upgrade-insecure-requests;"
I'm not sure if the above two issues are related, but I'd like to fix both.
With your shown samples, please try following Rules. Please make sure your htaccess Rules file is besides your cgi-bin directory/folder. Also clear your browser cache before testing your URLs.
RewriteEngine ON
##Rules for applying https to urls.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Rules to remove www from urls.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)$ [NC]
RewriteRule ^ https://%1/%{REQUEST_URI} [NE,R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^cgi-bin/main.cgi/page2/?$ cgi-bin/templates/page2/index.html [NC,L]
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.
Hoping someone with more htaccess experience can help us with this. We have Drupal 7 site that we have just moved from a dev to the live host (different hosting companies). However, now when someone puts a url with no protocol directly into the address bar (for example: examplesite.com/members), the page redirects to examplesite.com/index.php. I have been muddling around trying to fix this in the htaccess file, but have not been able to find the proper syntax for allowing urls with no protocol, while also forcing https://.
Our code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
We have tried moving the RewriteRule ^ index.php [L] line to underneath all rules, or commenting it out. This fixed the initial problem, but breaks the drupal admin functionality on the backend (can't see the admin menu, can't save anything, etc)
Any insight would be helpful, let me know if more info is needed. Thank you in advance.
You need to move the entire block, conditions + rule to the bottom. Drupal routes everything through the index.php script and it requires the previous 3 conditions in order to do that properly. If you simply move the RewriteRule line, then the conditions are all gone.
RewriteEngine On
# this needs to come first
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# then drupal stuff comes LAST
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
I have a small blog in generated with Jekyll up on a shared Apache server. I have been following this guide to set up the server so that I can update the site by pushing changes via GitHub, but I haven't even gotten that far yet. The .htaccess rewrite rule to point the domain to the generated /_site subdirectory has me stumped. The code I'm using (below) is redirecting the site correctly, but the guides I've read say that this code should hide the subdirectory from the URL, and this is not happening. It works correctly on the homepage, but subpages still have /_site/ in their URL. Any ideas?
My website
.htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteCond %{REQUEST_URI} !^/_site/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /_site/$1
RewriteCond %{HTTP_HOST} ^(www.)?joejoiner.tk$ [NC]
RewriteRule ^(/)?$ _site/index.html [L]
Add a trailing / to the end of line 6 in the above code block, like so:
RewriteRule ^(.*)$ /_site/$1/
I am looking to permanently redirect all the pages on a domain to one page on the SAME domain, using htaccess/mod_rewrite.
Basically I want any page requested for the domain to return a holding page [ which is index.php] at domain.com/
most of my attempts so far are causing errors as they are throwing the server intoa loop.
Thanks in advance
.k
You need to exclude the destination you are redirecting to like this:
RewriteEngine on
RewriteRule !^index\.php$ /index.php [L,R=301]
If you just want to redirect requests that can not be mapped to existing files in the filesystem, add this condition:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule !^index\.php$ /index.php [L,R=301]
But you should rather respond with a 404 in that case.
Here's a simple implementation:
RewriteEngine on
RewriteRule !\.(js|ico|gif|jpg|png|css|html)$ index.php
And a bit more sophisticated one:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Both were taken from Zend Framework Zend_Controller Programmer's Reference Guide. Here's another useful doc Apache Module mod_rewrite.
ISR that this was asked here recently - but I can't find the q right now.
Why not just set up the 404 errorhandler to point to the holding page (and keep all other content out of the doc root)
C.
Thanks to all,
got it working, here it is it might be of help to others:
RewriteRule !^(index.php)|.(js|ico|gif|jpg|png|css|mp3|xml|swf)$ /index.php [L,R=301]
.k