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.
Related
I have a site that is built on concrete5 (not by me). Now, I added a few lines to the htaccess to add the trailing slash after my urls as the site was loading over both versions.
However, now I cannot upload files to the site. I am just getting errors. There is also an option for users to upload a photo and they get an error too.
The only work around I have is that I can go into htaccess and comment out the lines I added, save it and then upload and the remove the #'s. Whilst this lets me upload in the short term, it's not a viable solution as regular users cannot do this.
My .htaccess is the following:
# -- concrete5 urls start --
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !index.php
RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# -- concrete5 urls end --
I comment out these lines for it to let me upload:
# -- concrete5 urls start --
RewriteEngine On
<IfModule mod_rewrite.c>
RewriteBase /
#RewriteCond %{REQUEST_URI} !(.*)/$
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_URI} !index.php
#RewriteRule ^(.*)$ /$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
# -- concrete5 urls end --
Whilst it looks like that i can upload but lose the trailing slashes so the urls are all messed up.
Anyone know what is going on? FYI I am not a developer but can understand to a degree.
Have you updated all your internal links (and form submission URLs) to include the trailing slash?
My guess is that you are submitting the form to a URL that does not include the trailing slash. The 301 redirect that follows will lose any submitted POST data (as the request is converted to GET).
You need to make sure that you are submitting your form to the canonical URL.
Well for a while I have been using codeigniter, but with the appeance of noone ever buying it. I don't see it being continued as far as development. That said, I am breaking away finally and wanting to go back to my roots so to speak, and hone in some of my skills. However I am unfortunately not that great with htaccess rules, and I became spoiled by codeigniter handling it for me with a couple lines of like below
#Removes index from url
RewriteCond $1 !^(index\.php|uploads|temp_pix|assets|qrgen|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /index.php?$1 [L]
What I want to do, is push everything through the index.php similar to your normal MVC/CMS.
If its not a valid file/directory. I want to then assume its But I do not want php/html/htm showing up in the URL. I'd also like to keep using query strings on occasion ie: domain.com/something/?foo=bar&bazz=who_knows. I also want to force trailing slashes when applicable, and force www
Now I have been digging for a bit and attempting combination after combination but I haven't come up with anything that really works
This .htaccess should take care of all of your requirements:
# to support extension-less URIs
Options +MultiViews
RewriteEngine On
# to enforce www in domain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
# to enforce trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
# internal rewrite to send all non files/directories to index.php as a query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?$1 [L,QSA]
I am trying to accomplish the following but my code is not working
1) I want to do either non-www to www or www to non-www
2) desktop users should be redirected to example.com/homepage
3) remove or hide /homepage from url
4) redirect mobile users to example.com/m
here is my code, it's not working, too many problems to list, but off the top of my head, I can think of 2 problems that I encountered: "cannot open page because too many redirects occurred" and mobile users are taken to /homepage instead of /m
# remove www
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [NE,R=301,L]
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
RewriteCond %{HTTP_USER_AGENT} ^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{REQUEST_URI} !^/m/.*
RewriteRule ^.*$ http://example.com/m [R=301,L]
Please help me, I am so frustrated, what am I supposed to change about this code to get it to work?
Unfortunately im no genious with regex or redirects in htacces but I have gotten similar code to work myself.
It does sound like your code is getting stuck in a redirect loop and the browser is stopping it from continuing.
I know thats not a complete answer but Hopefully that helps you make more sense of the code atleast a little.
Hopefully someone else can chime in otherwise i can dig up the code that is working when im on the computer and hopefully give you a better response.
-Sent from my cell
The www/non-www is fine.
For the homepage stuff, you need to do two things: make sure what you're rewriting into the homepage directory actually exists (otherwise you get a 500 server error) and exclude mobile user agents.
So this stuff:
RewriteEngine On
RewriteRule ^$ /homepage [L]
RewriteEngine On
RewriteRule ^$ homepage/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ homepage/$1
Needs to look like this:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} !^.*(ip(ad|od|hone)|blackberry|iemobile|android).*$ [NC]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/homepage%{REQUEST_URI} -d [OR]
RewriteRule ^(.*)$ homepage/$1 [L]
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]
First of all, I know there are plenty of similar questions about this around, but
None of them seem to work for me
None of them actually address exactly what I want
What I want is, as the title suggests, to redirect URLs without the .php extension to the actual .php file - changing the URL if possible (which I presume is just handled by [R=301]). The latest thing I tried was this:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ $1.php [R=301]
That doesn't work. I can still cant access /about.php with /about. (.htaccess rules themselves are working fine though)
I understand RegEx fine, but htaccess rules just mess with my head =[
So what should I do?
Now I know what you're thinking
One of you will say this: "Why do you want to do this? Just get rid of extensions completely and access your pages via /about or /about/ with a trailing slash."
I'd like to do that, it looks quite good. Problem is SEO - from which I assume my page ranks will get annihilated because all of a sudden they're on different URLs. So before you suggest that, suggest how I'd keep my page ranks first.
What I'm actually doing is essentially URL shortening for a poster - it's a lot easier for people to remember mywebsite.com/about than mywebsite.com/about.php.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## hide .php extension
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R=302,L]
# To internally forward /dir/foo/ to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
Please Make sure you have MultiViews options disabled using: Options -MultiViews
Beware of Apaches multiviews
Once you verify it is working fine, replace R=302 to R=301. Avoid using R=301 (Permanent Redirect) while testing your mod_rewrite rules.
Please make sure that there's mod_rewrite on your Apache HTTP Server and try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ /$1.php [R]
But clear your cache or use another browser first before checking the redirecting dynamic URLs, because you've been previously used the [R=301] flag! For more info. about that, please visit: https://stackoverflow.com/a/15999177/2007055
Could you try this one but it's quite the same as the previous code:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when it works, try adding these two conditions above the rewrite rule:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ http://%{HTTP_HOST}/$1.php
And when any of these codes above does not work, I think there's a problem in your Apache HTTP Server.
That works for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
You can chain it if you want e.g.
RewriteEngine On
# Remove trailing slashes.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [R=permanent,QSA]
# Redirect to HTML
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
# Redirect to PHP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
# Redirect to ASP
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.asp -f
RewriteRule ^(.+)$ $1.asp [L,QSA]