I'm trying to redirect all URLs to https except ones that start with /feeds/. This is an ExpressionEngine site, so I also have to remove index.php from the beginning of the URLs. The hosting setup also requires that ? be at the end of index.php. Here's what I have:
RewriteEngine On
RewriteBase /
# Force SSL for everything but feeds
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !^/(index.php\?*/)*feeds/ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$2 [R,L]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But when I go to http://www.mysite.com/feeds/blog, it gets redirected to https://www.mysite.com/?/feeds/blog.
I don't understand why this is happening. Here's the course of events as I see it:
http://www.mysite.com/feeds/blog is using port 80, so it meets the first condition
http://www.mysite.com/feeds/blog starts with /feeds/, so it fails the next condition and exits the rule
http://www.mysite.com/feeds/blog is not a file or directory, so it meets the next 2 conditions.
http://www.mysite.com/feeds/blog is changed to http://www.mysite.com/index.php?/feeds/blog and loops back to the top.
http://www.mysite.com/index.php?/feeds/blog is using port 80 and it starts with /index.php?/feeds/, so it fails the condition again and exits the rule
http://www.mysite.com/index.php?/feeds/blog does exist, so it fails the next 2 conditions and exits the rule.
The final server-side URL is http://www.mysite.com/index.php?/feeds/blog, and the client-side URL is still http://www.mysite.com/feeds/blog.
What's happening here? Where's that ? segment coming from?
Try this corrected code:
RewriteEngine On
RewriteBase /
# Force SSL for everything but feeds
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !/feeds/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R,L,NE]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ /index.php?/$1 [L]
Related
I want on my wordpress force only one slash at the end of some post or page url.
When WP page is not cached, I am in admin, is all ok.
Situations like:
somewww.com/somepage
somewww.com/somepage//
somewww.com/somepage///
all is forced to somewww.com/somepage/ where is only one slash.
But if we are on cached version of page, then urls are opening without forcing 1 slash at the end.
So, I've finded second code (on WP rocket website):
# Force trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} GET
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !\.(gif|jpg|png|jpeg|css|xml|txt|js|php|scss|webp|mp3|avi|wav|mp4|mov)$ [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1/ [L,R=301]
In htaccess I writed code at begining.
So, currently working well only situation number 1, other failed.
Who can help me with page urls rewriting on Wordpress?
Thanks
You may use this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{REQUEST_URI} !/$ [OR]
RewriteCond %{THE_REQUEST} \s[^?]*/{2,}[\s?]
RewriteRule ^(.*?)/?$ https://%{HTTP_HOST}/$1/ [L,R=301,NE]
Make sure to clear your browser cache before you test this rule.
I have the following mod rewrite rules - the first to remove the 'index.php' segment from URI requests and the second to force a HTTPS connection. However the second is reinserting the 'index.php'.
# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
# force SSL
RewriteCond %{HTTP_HOST} \.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
For example -
http://www.mysite.tld/somepage
becomes
https://www.mysite.tld/index.php?/somepage
I'm still trying to understand how to write htaccess files but is there a way to combine these two to avoid this issue?
You need to swap the order of your rules.
# force SSL
RewriteCond %{HTTP_HOST} \.
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# send request via index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
When the redirect rule was last, it was still being applied because the first rule gets applied, rewriting stops for that iteration, then the rewrite engine loops, then the second rule gets applied and the URI (which now contains an index.php) gets redirected.
We have a rewrite rule that looks like this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
Which works as expected; if the file does not exist, forward to page.php which is a generic page and allows for pages with custom URLs.
We also want to force the www. to precede the domain: example.com -> www.example.com
For that, we use:
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
This also works, but when the two are supposed to work together:
example.com/non-existent-file
We end up getting:
www.example.bom/page.php
Which defaults to our 404 page. How do I modify these rules to work together? I have tried playing with some of the different suffic characters (ie. removing the L in the [NC,L] in an attempt to get both rules to process).
I have also tried repeating the forward to page.php lines beneath the www redirect (and removing the L suffix) with no success.
The entire HTACCESS file looks like:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [R=302,L]
Also, is there a %{CONSTANT} I can use in place of example.com that represents the current domain?
The constant you're looking for is %{HTTP_HOST} as provided by the HTTP request. Perhaps you are looking for the variable %{SERVER_NAME} for the virtual host's default servername?
Place the hostname checking rule first, and use a RewriteCond in the page.php rule to only apply if the domain is already correct:
RewriteEngine On
# First rewrite the domain
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# Then rewrite the files
# At this point, the domain should already have been enforced
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule (?!^(cms|images|js|mobile)(/.*|)$)^.*$ page.php [NC,L]
I'm trying to change the word "portfolio" in my URLs to "portfolio/project" and inadvertently created a redirect loop. Would appreciate any help in pointing me in the right direction.
Example:
http://www.example.com/portfolio/interactive/abc/ to
http://www.example.com/portfolio/project/interactive/abc/
Current htaccess (last two lines relates to issue):
redirect 301 "/sitemap.xml" http://www.example.com/sitemap.php
RewriteEngine On
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/sitemap.php
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]
Your problem is that your regex also matches your target, so after the redirect, the URI matches the same rule and gets redirected again (you may have noticed that there's a bunch of /project/project/project/project/project/project/project in the URI)
Add an exclusion condition:
RewriteCond %{REQUEST_URI} !portfolio/project
RewriteCond %{REQUEST_URI} /.*portfolio.*$ [NC]
RewriteRule ^(.*)portfolio(.*)$ /$1portfolio/project$2 [R=301,L]
I've been using the below code in my .htaccess file for a while to make the EE URLs work without needing index.php in the URL. What I've found though it that I'm getting some reports from crawling tools that I'm getting duplicate content as /lorem/ipsum/ is also popping up somewhere as /index.php/lorem/ipsum/.
I know that this is likely a result of a stray link referencing the index.php in the URL but I'd like to close up the gaps by forcing index.php out of the links. I've had a look around but I can't seem to find how to force it out.
RewriteEngine On
RewriteBase /
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Sure thing.
# Enable Rewrite Engine
# ------------------------------
RewriteEngine On
RewriteBase /
# Redirect index.php Requests
# ------------------------------
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteCond %{THE_REQUEST} ^GET
RewriteRule ^index\.php(.+) $1 [R=301,L]
# Standard ExpressionEngine Rewrite
# ------------------------------
RewriteCond $1 !\.(css|js|gif|jpe?g|png) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]