Adding .html onto the end of a URL - wordpress

I'm currently using Wordpress as a blogging platform but I want to change to use Jekyll to generate the static pages. On Wordpress my URLs use the following format:
/year/month/day/title
but I want to redirect that to
/year/month/day/title.html
and I can't figure out how to do that using mod_rewrite.
Anyone got any ideas?

RewriteEngine On
# Only if the URI is not a normal file
RewriteCond %{REQUEST_FILENAME} !-s
# ... or a symbolic link
RewriteCond %{REQUEST_FILENAME} !-l
# ... rewrite everything that ends on .html to the stripped down URL
RewriteRule (.+)\.html$ $1 [L]
# Alternatively, if you want to be more specific about the scheme, you can use this
# RewriteRule ^/([0-9]{4})/([0-9]{2})/([0-9]{2})/([^/]+)\.html$ $1/$2/$3/$4 [L}
The above should give you some pointers on how to properly rewrite the URL to the scheme you desire. This example transparently rewrites everything that ends on .html (except actual files) to the same URL without the .html appended to it.

I believe you can just go to Admin → Settings → Permalinks and set the permalinks to custom with a value of:
/%year%/%monthnum%/%day%/%postname%.html

Related

url masking via htaccess rewrite is not working

Not sure if this is the right section of Stackoverflow to ask my question...
But here it is:
So I am using the below on the .htaccess file:
RewriteEngine On
RewriteRule ^sale?$ /discount-page/
So that when people visit example.com/sale page, they see content from example.com/discount-page/
But when I visit example.com/sale it shows 404 error saying that the URL /discount-page is not available on this server...
Why is it happening?
Here's how my entire .htaccess file looks like:
# 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
Can anyone help please?
When using WordPress, you can't simply rewrite the URL in .htaccess to the %postname% (the real URL) since WP still looks at the REQUEST_URI in order to route the URL. Even though you are rewriting /sale to /discount-page/ (the actual URL), WordPress sees /sale (the requested URL) - which doesn't exist inside WP; hence the 404.
Although not your intention, you could change this to an external redirect to get around this problem (which also avoids a potential duplicate content issue). For example:
RewriteRule ^sale$ /discount-page/ [R,L]
(I removed the ? in ^sale?$, as that does look erroneous. Or do you really want to match /sale or /sal?)
Alternatively, you could try rewriting to the underlying "plain" permalink. ie. explicitly pass the %post_id%. This is different to rewriting to the %postname%, since WP shouldn't need to check the REQUEST_URI in order to route the URL. For example:
RewriteRule ^sale$ /index.php?p=123 [L]
Where 123 is the %post_id% of your discount-page. By rewriting directly to index.php, you are effectively bypassing WP's front-controller.
Note that this must go before the standard WordPress directives in .htaccess (aka the front-controller).
However, I still feel there should be a more WordPress-like way of doing this, which is why I initially suggested asking this question over on the WordPress Stack. However, if you do that, don't mention ".htaccess". What you are really creating is a URL alias or something like that. For example: Have two different URLs show the homepage

Apache rewrite to map # based URL into proper URLs

Long version (you can skip to TL;DR if you want to):
I am working with a Wordpress site that was set up by someone else. The website has multiple pages where page has tabbed content which is accessible through #. For eg:
www.example.com/services/category1/#tab-service1
www.example.com/services/category1/#tab-service2
www.example.com/services/category2/#tab-service1
www.example.com/services/category2/#tab-service2
www.example.com/services/category2/#tab-service3
Now, when search engines index they are indexing only www.example.com/services/category1/ and www.example.com/services/category2/. This creates a problem where we cannot have search engines point directly to the content within a given tab. What we want is for search engines to show links that takes users directly to (say) www.example.com/services/category2/#tab-service3.
Now, I don't think google can index such # based content on its own. So, I am thinking of using apache rewrites to try to resolve this issue. I have access to .htaccess file only (from a config perspective).
TL;DR
How to redirect www.example.com/services/category1/service3/ to www.example.com/services/category1/#tab-service3 using apache redirects (I have access to .htaccess file)?
This is what I am trying but it's not working:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9])/? [NC]
RewriteRule .* /services/category1#tab-%1 [R,NE,L]
Someone also adviced to look into pushState server config to fix this. I am not sure how to use pushState.
UPDATE:
I have updated the rewrites to the following but it still doesn't work. It keeps showing Wordpress' 404 page
<IfModule mod_rewrite.c>
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /domainfolder/
RewriteCond %{REQUEST_URI} ^/services/category1/([a-z0-9]+)/?$ [NC]
RewriteRule ^/services/category1/([a-z0-9]+)/?$ /services/category1/#$1 [NE,R,L]
</IfModule>
Your %{REQUEST_URI} regex is wrong. The pattern ^/services/
category1/([a-z0- 9 ])/? matches /services/category1/{any 1 char of a-z or 0-9} format followed by an optional slash. So this does not match your request /services/category1/service3 but matches /services/category1/a/ .
You should be using
^/services/category1/([a-z0-9]+)/?$

Convert .html to -html in file name

I converted my website to WordPress. In the process, in order to satisfy WordPress's rules about permalink formats, I had to change the filenames from ending in .html to ending in -html.
Unfortunately, there are backlinks from other sites that point to the old pages that end in .html. So when someone clicks one of those backlinks, the page cannot be found on the new site.
For this reason, I want to permanently redirect all requests for URIs ending in .html using .htaccess so that they end instead with -html.
So for instance:
https://example.com/file1.html
needs to permanently redirect to
https://example.com/file1-html
Sadly, my code generates 500 errors.
Here's what I wrote based upon what I found in the Apache manual:
<Directory /home/accountname/public_html>
RewriteEngine on
RewriteBase /home/accountname/public_html
RewriteCond $1.html !-f
RewriteRule ^(.*).html$ $1-html [R=301,L]
</Directory>
Can someone help?
<Directory> directive isn't allowed in .htaccess.
Place this rule in your main WP .htaccess:
RewriteEngine on
RewriteRule ^(.+?)\.html$ /$1-html [R=301,L,NE,NC]
Make sure this is first rule below RewriteEngine on line.

Modify friendly URLs generated by default in Wordpress (via .htaccess)

I have a website done in Wordpress and I need to make some changes in the fiendly URLs.
I’ve created a page from the admin panel named detail, this page reads the template file detail.php from the templates folder.
The URL that is currently mounted is http://www.domain.com/detail/1234/ and I need that it could be accessed as http://www.domain.com/anything/1234/.
The following lines have been generated by Wordpress but I don’t understand them and I don’t know how to modify them for my purpose:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
First you should really understand what those rules are doing and what you really want to achieve. Then you can try to change the system to fit your needs.
IfModule ensures everything inside is processed only when mod_rewrite Apache module is present. All the other directives are from this module.
RewriteEngine On enables URL rewriting.
RewriteBase / tells the engine that the rules operate inside root. See also the general question on how RewriteBase works.
RewriteRule ^index\.php$ - [L] means that no more rules should be processed ([L]) if the current URL to be rewritten is index.php. No rewrite takes place. RewriteRule directive accepts a regex. See also regex tag here on SO.
All RewriteCond directives apply to the following RewriteRule. Unless [OR] flag is added, they must be all satisfied at the same time to execute the rule. In this case they mean:
Requested resource is not a regular file.
Requested resource is not a directory.
Rewrite any (at least one character long) URL to index.php. This is the last ([L]) rule to be processed.
When adding new RewriteRules, you probably want to use the WordPress way of doing this, as described in Zac’s answer. Figuring out the right rule by analogy to the many examples in the manual or here on SO should not be hard.
Put into functions.php maybe a better idea:
functions.php
function setRewriteRule($orgRules){
return array( '/([^/]+)/([0-9]+)/?' => 'index.php?post=$matches[1]' ) + $orgRules;
}
add_filter('rewrite_rules_array', 'setRewriteRule');
Then you just need flush the rewrite rules, I usually use 'rewrite-rules-inspector' plugin.
This should solve your problem give it a try ... you can write your own custom permalink without adding any code also if someone tries to access the page via old URL they will be redirected to the new one.
WordPress Custom Permalinks
After installing this you just have to go into pages and type your own URL below the heading

Hide Page Extensions (Like StackOverflow)

I want to hide page extensions like stackoverflow does. How does the following work?
http://stackoverflow.com/tags/foo
http://stackoverflow.com/tags/bar
I've seen a lot of sites that do this, but I still don't know how this is accomplished (I have a LAMP stack).
When a web server gets a request for a URL, it has to decide how to handle it. The classic method was to map the head of the URL to a directory in the file system, then let the rest of the URL navigate to a file in the filesystem. As a result, URLs had file extensions.
But there's no need to do it that way, and most new web frameworks don't. They let the programmer define how to map a URL to code to run, so there's no need for file extensions, because there is no single file providing the response.
In your example, there isn't a "tags" directory containing files "foo" and "bar". The "tags" URL is mapped to code that uses the rest of the URL ("foo" or "bar") as a parameter in a query against the database of tag data.
What you want is clean URLS and you can do it with apache and .htaccess . There may be a better way, but here's how I have been doing it:
http://evolt.org/Making_clean_URLs_with_Apache_and_PHP
That's the beauty and the work of ASP.NET MVC.
No "hiding" - it's just the way ASP.NET MVC handles URL's and maps those "routes" to controller actions on your controller classes.
Quite a big step away from the "classic" ASP.NET Webforms way of doing things.
There are a couple of ways to do it under Apache+PHP, but the essential principle is to make a set of URIs (perhaps all URIs, depending on your site, but you may want different scripts to handle different portions of the site) translate to a single PHP file, which is told what object the user has requested.
The conceptually simplest way is to rewrite every URL to a script, which gets the URI through $_SERVER['REQUEST_URI'] and interprets it as it likes.
The URI rewriting can be done with various methods including mod_rewrite, mod_alias and ErrorDocument (see Apache docs).
Another way is to set up more complex URL rewriting (probably using mod_rewrite) to add the path as a GET variable.
There is also the $_SERVER['PATH_INFO'] variable which is loaded with the non-existent portion of the path. This option requires little or no modification to Apache config files, but reduces the flexibility of your URLs a little.
Modern web development frameworks have support for elegant urls. Check out Django or Ruby on Rails.
If you're using Apache and you simply want to hide the file extensions of static HTML files you can use this .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f # if the requested URL is not a file that exists
RewriteCond %{REQUEST_FILENAME} !-d # and it isn't a directory that exists either
RewriteCond %{REQUEST_FILENAME}\.html -f # but when you put ".html" on the end it is a file that exists
RewriteRule ^(.+)$ $1\.html [QSA] # then serve that file
</IfModule>
Apache mod_rewrite has been called "voodoo, but seriously cool voodoo".
The actual .htaccess code I use on a few sites is like that, but not identical:
<IfModule mod_rewrite.c>
RewriteEngine on
#RewriteRule ^$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1\.php [QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [QSA]
</IfModule>
And here is some much longer but far more readable code to do the same thing on a Zeus server. On Zeus, it's called rewrite.script.
# http://drupal.org/node/46508
# get the document root
map path into SCRATCH:DOCROOT from /
# initialize our variables
set SCRATCH:ORIG_URL = %{URL}
set SCRATCH:REQUEST_URI = %{URL}
match URL into $ with ^(.*)\?(.*)$
if matched then
set SCRATCH:REQUEST_URI = $1
set SCRATCH:QUERY_STRING = $2
endif
# prepare to search for file, rewrite if its not found
set SCRATCH:REQUEST_FILENAME = %{SCRATCH:DOCROOT}
set SCRATCH:REQUEST_FILENAME . %{SCRATCH:REQUEST_URI}
# check to see if the file requested is an actual file or
# a directory with possibly an index. don't rewrite if so
look for file at %{SCRATCH:REQUEST_FILENAME}
if not exists then
look for dir at %{SCRATCH:REQUEST_FILENAME}
if not exists then
look for file at %{SCRATCH:REQUEST_FILENAME}.php
if exists then
set URL = %{SCRATCH:REQUEST_URI}.php?%{SCRATCH:QUERY_STRING}
else
set URL = /index.php/%{SCRATCH:REQUEST_URI}?%{SCRATCH:QUERY_STRING}
endif
endif
endif
goto END

Resources