I need to modify my .htaccess file to redirect all URLs to a HTTPS version without the "www".
http://example.com --> https://example.com
http://www.example.com --> https://example.com
https://www.example.com --> https://example.com
This is how my .htaccess file looks like:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ redirect.php?id=$1&page=$2 [QSA,L]
What modifications do I need to make to .htaccess in order to forward to HTTPS without the "www"?
It depends on what you are doing with your current code. That just looks like a file that is called redirect but is trying to display a php page from a SEO friendly URL. So I think that might be confusing people.
Anyway, to do what you need to do and force https without www, you only need one more rule above.
Also your second rule needs two capture groups in the RewriteRule test string because you are wanting two different values using $1 and $2 back references.
RewriteEngine On
#rediect www to non www and/or http to https --- all combinations.
RewriteCond %{HTTP_HOST} !^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://example.com%{REQUEST_URI} [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)/(.+)/?$ redirect.php?id=$1&page=$2 [QSA,L]
So this second rule will work for an URL formed like this.
https://example.com/123/pagename
Related
I have the following problem
I need two things:
Redirect from unsafe HTTP to safe HTTPS and then
Redirect from https://example.com/index.html to https://example.com/work-for-us
I know the first one I can obtain using:
RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
and the second one using
RewriteEngine on
RewriteRule work-for-us index.html
But is it possible to connect those two together?
So I have both HTTP -> HTTPS and /index.html -> /work-for-us
I don't have too much knowledge about .htaccess file, I read something on the internet but didn't find anything that answers my question.
So putting everything together, I want to change the URL from this:
http://example.com/index.html to this https://example.com/work-for-us
Thank you in advance.
Based on your shown samples, could you please try following.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine on
##First rule for applying https to URLs.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
##Second rule for serving homepage(only dns link) with index.html file.
RewriteRule ^/?$ work-for-us [R=301]
RewriteRule ^/?$ index.html [L]
##Third rule for serving non-existing directories/files with index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
I tried some of the other answers I could find in here, but it didn't work out. It's really simple though.
I want
/page?id=PAGENAME
to be accessible AND redirected to
/PAGENAME
Can you help me?
EDIT:
It feels like my already messed-up .htaccess file needs to be included in here. I already have basic rewriting enabled, but this feature is needed for two other "special pages". In the requested solution above, I would therefore just replace "page" with the two pagenames (it's danish names, so I thought it was easier this way).
Currently I have this. If you have any improvements to it, it's appreciated - but I just want this to work with the requested solution aswell.
# Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
# Always on https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# remove trailing slash
#RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
#301 Redirect everything .php to non php
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^.]+\.)+php?\ HTTP
RewriteRule (.+)\.php?$ http://MYURL.dk/$1 [R=301,L]
#Hide the .php from url
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
#301 Redirect everything mistype after file extension -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
#301 Redirect everything to current url -
RedirectMatch permanent /(.*).php/.* http://MYURL.dk/$1.php
RewriteCond %{REQUEST_FILENAME} -D
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L]
#301 Redirect from non www to www
RewriteCond %{HTTP_HOST} ^www.MYURL.dk [NC]
RewriteRule (.*) http://MYURL.dk/$1 [R=301,L]
#301 redirect index.php to /
RewriteBase /
RewriteCond %{REQUEST_URI} index.php
RewriteRule .* http://MYURL.dk/ [R=301,L]
#Deny access to songs
RewriteCond $1 !(loadmedia)\.php
RewriteRule ^songs/(.*)$ - [L,F]
Generally the URL in address bar should be like
www.siteurl.com/pagename/ for seo purpose and then read this url from .htaccess using rule which gives this query string parameter values in your php file.
.htaccess rule can be like
RewriteRule ^(.*)/$ /page?id=$1 [QSA,L]
It looks like you are wanting to implement "friendly" (or "pretty") URLs, making the URLs more friendly for you users (search engines don't really mind what your URLs look like).
The first step is to change all your on-page links to use the new "friendly" URL. So, you links should all be of the form /pagename (not /page?id=PAGENAME).
Then, in .htaccess, you need to internally rewrite this "friendly" URL into the real URL that your server understands. This can be done using mod_rewrite. In the .htaccess file in your document root:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ^([\w-]*) /page?id=$1 [L]
If the file does not exist (!-f) and does not contain the id URL param then internally rewrite the request from /<pagename> to /page?id=<pagename>. This assumes your <pagename> consists only of the characters a-z, A-Z, 0-9, _ and -.
If this is a new site and the old URLs are not already indexed or referenced by external sites then you can stop here.
However, if you are changing an existing URL structure then you also need to externally redirect the real (ugly) URL to the "friendly" URL before the above internal rewrite. (This is actually what you are asking in your question.) In order to prevent a rewrite loop we can check against %{THE_REQUEST} (which does not change when the URL is rewritten).
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
Change the 302 (temporary) to 301 (permanent) when you are sure this is working OK. Permanent redirects are cached by the browser so can make testing a problem.
So, in summary, with the above two parts shown together:
# Enable the rewrite engine
Options +FollowSymLinks
RewriteEngine On
# Redirect real URLs to "friendly" URLs
RewriteCond %{THE_REQUEST} \?id=([\w-]*)
RewriteRule ^page$ /%1? [R=302,L]
# Rewrite the "friendly" URL back to the real URL
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !^id=
RewriteRule ([\w-]*) /page?id=$1 [L]
The order of directives is important. External redirects should nearly always come before internal rewrites.
UPDATE#1:
I want /concept?id=NAME to go to /NAME and /studio?id=NAME to go to /NAME - there's 5-10 different "pages" from both concept and studio. [Corrected according to later comment]
Since id=NAME maps to /NAME you can achieve all 10-20 redirects with just a single rule:
RewriteCond %{QUERY_STRING} ^id=(NAME|foo|bar|baz|abc|def|ghi)
RewriteRule ^(concept|studio)$ /%1? [R,L]
This will redirect a URL such as /studio?id=foo to /foo.
As with all external redirects this should be one of the first rules in your .htaccess file.
Change R to R=301 when you have tested that it is working OK.
To make this more "dynamic", ie. match any "NAME" then change the CondPattern, for example:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
UPDATE#2:
If the path part of the URL (ie. concept or studio) is required then you can modify the RewriteRule substitution like so:
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^(concept|studio)$ /$1/%1? [R,L]
Which will redirect /concept?id=foo to /concept/foo.
Or, to be completely "dynamic" (bearing in mind this will now capture anything):
RewriteCond %{QUERY_STRING} ^id=([\w-]*)
RewriteRule ^([\w-]+)$ /$1/%1? [R,L]
Here is my Problem
I have 10 URLS on a website A
I want to redirect 2 URLS (out of 10) from Website A to Website B via .htaccess
I want that 4 URLS from the list should never be redirected to anywhere
I want that remaining 4 URLS should be redirected to Website C
And Anyother URL from website A should redirect to Website D
Can any body help ?
Redirect /percussion/21021-crossing-grip-extensions http://myblog.tumblr.com/thisURL
Redirect /percussion/21045-indoor-percussion-circuits http://myblog.tumblr.com/thisURL
Redirect /percussion/21047-26-standard-rudiments-on-social-media http://myblog.tumblr.com/thisURL
RewriteCond %{REQUEST_URI} !^/wind-instruments/38250-hd200-excerpt-video
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49480-gc1ta
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49485-u1ta-2015
RewriteCond %{REQUEST_URI} !^/namm-2015-products/49486-a6r-a-series-2015
RewriteRule ^(.+)$ http://www.codephun.com/$1 [R=301]
You have a huge .htaccess file. I will provide few tips that you can use to fix your problem.
Don't use Redirect directive, just have your rules using RewriteRule itself.
Use THE_REQUEST variable instead of REQUEST_URI as THE_REQUEST variable that represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules.
So for the snippet shown above use:
RewriteEngine On
RewriteRule ^percussion/21021-crossing-grip-extensions http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteRule ^percussion/21045-indoor-percussion-circuits http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteRule ^percussion/21047-26-standard-rudiments-on-social-media http://myblog.tumblr.com/thisURL [L,NC,R=302]
RewriteCond %{THE_REQUEST} !/wind-instruments/38250-hd200-excerpt-video
RewriteCond %{THE_REQUEST} !/namm-2015-products/49480-gc1ta
RewriteCond %{THE_REQUEST} !/namm-2015-products/49485-u1ta-2015
RewriteCond %{THE_REQUEST} !/namm-2015-products/49486-a6r-a-series-2015
RewriteRule ^(.+)$ http://www.codephun.com/$1 [R=301]
# Rest of the Joomla rules come here
A site of mine has all the files stored in a subdirectory. Previously, I used a 301 redirect to send visitors from site.com/specific/page to site.com/subdirectory/specific/page.
I am now using this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteCond %{REQUEST_URI} !^/subdirectory/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /subdirectory/$1
RewriteCond %{HTTP_HOST} ^(www.)?site.com$
RewriteRule ^(/)?$ subdirectory/index.php [L]
This means the URL is cleaner. However, is there a way to make the htaccess redirect people using the subdirectory to the cleaner URL? i.e. if someone goes to site.com/subdirectory/specific/page, is there a way to make the URL site.com/specific/page, while showing the content from site.com/subdirectory/specific/page?
Yes, buit instead of rewriting, youn would redirect..
RedirectMatch 301 "^/subdirectory/(.*)" "/$1"
That way, if they hit any subdirectory directly, they will be redirected to the cleaner URL, where your rules above will kick in.
We just redesigned a site for a client in EE, located at example.com (with and without www.). Their original site is ASPX. They've still got a number of ASPX pages that they want to keep, so their IT people created a subdomain, www2, which is basically a clone of their old site.
I need an htaccess rule that will check if the requested page ends in .aspx, then redirects to the www2 subdomain. It should also make sure that the requested page doesn't exist
I tried using the following rule, but it doesn't work.
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
My htaccess file (including the above rule) looks like this:
RewriteEngine On
# redirect all .aspx pages to www2
RewriteRule ^http://[www\.?]example.com/(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]
# strip index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Does anyone have a solution for this?
The RewriteRule directive does only test the URL path. If you want to test any other part of the requested URL, you need to use the RewriteCond directive:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^(.*)\.aspx$ http://www2.example.com/$1 [R=301,L]