I am using the following to redirect my non www. urls to use www.:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://www.domain.com/"
</IfModule>
However, if someone visits domain.com/abc-123 it redirects to www.domain.com and loses the remaining of the url.
Any help with this? How can i set it up so it doesn't remove the end of the url?
Miro
Just swap the order of the rules around. Since the first (WP) rule is stripping the path, by the time the second rule is applied the path will be empty.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
<IfModule mod_headers.c>
Header set Access-Control-Allow-Origin "http://www.domain.com/"
</IfModule>
Related
Recently, we switched from www.example.deto www.example.com/de. Unfortunately some minor and bigger issue occurred with the .htaccess.
Issue
Redirect chain
www.example.de/path redirects to www.example.com/de/path an then to www.example.com/de/path/
How do I remove the unecessary redirect?
Every blog article gets redirected to http and then to https.
https://www.example.de/blog/article redirects to http://example.com/blog/article/ then to https://example.com/blog/article/ and finally to https://example.com/de/blog/article/
The redirect to http only occurs for the blog. The rest off the website doesnt redirect to http.
It seems to me that the htaccess is ignored by requests to the blog. I don`t know what I did wrong!
htaccess www.example.de:
SetEnv PHPRC /home/customer/www/example.de/public_html/php.ini
RewriteEngine on
RewriteRule ^(.*)$ https://www.example.com/de/$1 [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
htaccess www.example.com/de:
SetEnv PHPRC /home/customer/www/example.com/public_html/php.ini
# BEGIN rlrssslReallySimpleSSL rsssl_version[5.1.0]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Possible Solution
Can I add this to solve the Issue with the trailing slash?
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ https://www.example.com/%1 [R=301,L]
htaccess in root folder
#<IfModule mod_rewrite.c>
#RewriteEngine On
#Uncomment these two lines If you want to redirect http to https
#RewriteCond %{HTTPS} off
#RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ index.php [L]
#</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^https://3pmix.com/ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</IfModule>
htaccess in sub folder
RewriteRule ^classes/.*$ - [F,L]
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} config.php|db.php|functions.php [NC]
RewriteRule .* - [F,L]
directory structure through images
With your shown samples, please try following Rules file. Also make sure to clear your browser cache before testing your URLs.
Have your root htaccess Rule file like as follows:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^3pmix\.com [NC]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
</IfModule>
And have your sub directory htaccess Rules file as follows:
RewriteOptions InheritBefore
RewriteEngine ON
RewriteRule ^classes - [NC,F,L]
RewriteRule (?:config\.php|db\.php|functions\.php) - [NC,F,L]
I have also fixed flags and regex in your htaccess files.
Rewrite base was missing that is why was not finding the root url of the website
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php [L]
</IfModule>
So I'm trying to redirect incoming connections based on the referrer URL. So for instance if someone was trying to visit:
blog.example.com/category/fancy-stuff
They are already getting redirected to example.com but I want to redirect them to:
example.com/news/category/fancy-stuff
Right now I have this for my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
RewriteCond %{HTTP_REFERRER} blog.example.com/category/* [NC]
RewriteRule ^ example.com/news/category/*? [R=301,L]
</IfModule>
Is there a way to use variables or something to just append the proper category to the new URL?
Reorder your Rewrite rules :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_REFERRER} ^blog.example.com/category/.*$ [NC]
RewriteRule ^(.*)$ http://example.com/news/$1 [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
I would like to re-write this url here;
site.com/product.php?s=electricity-at-work-regulations
to
site.com/courses/electricity-at-work-regulations
so change product.php to courses and generally tidy the url up.
Is this possible?
.htacess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^peoplefactor.co.uk$ [NC]
RewriteRule ^(.*)$ http://peoplefactor.co.uk/$1 [L,R=301]
</IfModule>
# Clean URLS
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^blog/([a-zA-Z0-9-/]+)/preview$ /blog/post.php?s=$1&preview=all [L]
RewriteRule ^blog/([a-zA-Z0-9-/]+)$ /blog/post.php?s=$1 [L]
RewriteRule ^course/([a-zA-Z0-9-/]+)$ /course.php?s=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
</IfModule>
# Gets rid of www.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L]
</IfModule>
Yes but it would be better the other way around.
You should always call site.com/courses/electricity-at-work-regulations within your application and reroute it to site.com/product.php?s=electricity-at-work-regulations. Something like
RewriteEngine On
RewriteBase /
RewriteRule ^(courses)/([a-zA-Z\-]+)$ product.php?s=$2 [L]
I'm trying to redirect all requests to a preview site to a different site. I'm not sure why this is not working. I'm not being redirected when I click the link.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^site.preview.com$ [NC]
RewriteRule (.*) newsite.com/$1 [L,R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Answer is to remove the ! in the RewriteCond as this says only rewrite it if the the url is not equal to site.preview.com but I want it to redirect if that url is entered.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^site.preview.com$ [NC]
RewriteRule (.*) newsite.com/$1 [L,R=301]
</IfModule>
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress