RewriteEngine config - html

My website is coded on a way that instead send on from, input, etc thigs such as index.php?subtopic=register&step=1, index.php?subtopic=account&page=login, etc it sends out just a /account, or /register... I'm a amateur when it comes to web, but I searched a little and I started to make a .htaccess file like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?subtopic=$1 [L,QSA]
RewriteRule ^home/([^/]+)/?$ /index.php?subtopic=home [L,QSA]
RewriteRule ^characters/([^/]+)/?$ /index.php?subtopic=characters&name=$1 [L,QSA]
RewriteRule ^register/([^/]+)/?$ /index.php?subtopic=register&step=$1 [L,QSA]
It does seen to work, but soon I realized that there is too many variables to fill, it does not seem right.. Sorry for ask, I think that it should be very simple, but I'm not figuring out this..

From the comments to the question it becomes clear that your are asking for a more flexible way to handle the different tokens before the first slash ("characters" or "register").
You certainly can grab those tokens by a pattern too in a flexible manner:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /index.php?subtopic=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?subtopic=$1&name=$2 [L,QSA]
Certainly you will have to make sure that this strategy actually is correct for all requests matching that pattern. So you might have to add some exception to that general rule...
This would be a slightly modified version which I would recommend to prefer. Using an optional leading slash (^/? instead of just ^) allows to use the same rule set in the real host configuration which always is preferable to using .htaccess style files:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/?$ /index.php?subtopic=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/([^/]+)/?$ /index.php?subtopic=$1&name=$2 [L,QSA]

Related

Remove .html off URL. .htaccess

I'm building a site in a Node.js container, and the .htaccess is not working. I just want to remove the .html off the url, but the following just downloads the pages as files. Sorry if this has been answered, just need help. Thanks!
What we currently have:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301]
To remove .html all you need to use is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
EDIT:
Some hosting services require a slightly different layout:
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Apache Mod Rewrite for CodeIgniter AND HTML5 mode

I have Codeigniter running on Apache and have Apache Mod Rewrite that works well.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I've now changed the front-end to be HTML5 and my app uses emulated URLs that suppose to be handled by the same 1-page application.
So I need something like the following URL rewrite:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite everything else to index.html to allow html5 state links
RewriteRule ^ index.html [L]
So, I'd like to combine those together.
First I'd like to see if the first part of URL path is a CI controller (a file in ./application/controllers) and if yes then rewrite it to index.php.
If not, I'd like to rewrite it to index.html.
I'm trying to build something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# -- RewriteCond ^/([^/]+)/ (application/controllers/$1.php) is a file
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^ index.html [L]
I understand that my other option it to manually specify all possible routes and controllers but would be nice to do it automatically...
UPDATE:
The proposed solution below helped me to solve the problem.
This is what worked for me eventually:
RewriteCond C:/projects/someproject/WS/application/controllers/$1.php -f [OR]
RewriteCond C:/projects/someproject/WS/application/controllers/$1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/$
RewriteCond C:/projects/someproject/WS/application/controllers/%1 -d
RewriteRule ^(.+)$ index.php/$1 [L]
RewriteCond $1 ^(.*)/[^/]*/?$
RewriteCond C:/projects/someproject/WS/application/controllers/%1.php -f
RewriteRule ^(.+)$ index.php/$1 [L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.html [L]
I've realised I had to check for more types of URLs and also I found I couldn't use %{DOCUMENT_ROOT} because I'm using VirtualDocumentRoot settings and DOCUMENT_ROOT isn't working
Try :
# If /document_root/application/controllers/request.php is a file
RewriteCond %{DOCUMENT_ROOT}/application/controllers/$1.php -f
# Rewrite the request to /index.php
RewriteRule ^(.+)$ /index.php/$1 [NC,L]
# else, rewrite the request to /index.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.html

Removing .html file extension from URL with an .htaccess file

I've tried a number of different snippets inside my .htaccess file in order to remove the .html file extension from displaying in the URL. So far I've read some other answers on here that haven't worked, researched some personal blogs, and even found some tricks that didn't work on Chris Coyier's CSS Tricks.
Here's a couple of the ones I've tried:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
As well as:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.*)$ $1.html
I appreciate any help I can get.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

Need to remove .html file extension and duplicate names

I have a .htaccess file with the contents below, that removes the .html file extension for all of my website's pages.
Options +MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
RewriteEngine On
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]
My links now look like www.james-lee.io/resume/resume when before they looked like www.james-lee.io/resume/resume.html. I would like to remove the folder name so the name of the folder is not duplicated by the name of the file minus the .html and the final result looks like www.james-lee.io/resume.
I have seen similar questions but not exactly what I am looking for.
So I have done this task!
Try this code:
RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2 -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/(.*)$ /$1/$1 [END]
Now I try to explain this rules.
first line: you do request like /folder/file
second line: check if /folder/ real existing folder
third line: check if /folder/file is real existing file
fourth line: I use notation %1::%2 because backreferences can only
be used in the left part of RewriteCond. But it possible to reuse
left part in pattern of the right part. So, in the "^(.*)::\1$" I
check all before ::. Then I have result at the \1 backreference. So,
if folder is equal to file, text after :: will be equal to %2.
Next I just redirect to the result (/folder or /file, doesn't
matter, because both are equal)
But if folder == file, redirect will be always to the directory.
So, next I check, if redirect result is existing dir and change the link.
Request example:
http://yourdomain/test/test
(this will be redirected to http://yourdomain/test, but will reference to original link)
I hope, I explain clearly, but if you have any questions, I would glad to answer.
Thank you for insteresting task!
P.S. see also %N backreference inside RewriteCond
UPDATED. Your htaccess have to be like below:
RewriteEngine on
RewriteCond %{SERVER_PORT} !=443
RewriteCond %{HTTP_HOST} ^(www\.)?james-lee\.io$ [NC]
RewriteRule ^$ https://www.james-lee.io%{REQUEST_URI} [R,L]
RewriteCond %{REQUEST_URI} ^/(.*)/(.*)$
RewriteCond %{DOCUMENT_ROOT}/%1 -d
RewriteCond %{DOCUMENT_ROOT}/%1/%2\.html -f
RewriteCond %1::%2 ^(.*)::\1$
RewriteRule ^(.*)$ /%1 [R,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)/(.*)$ /$1/$1.html [END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

Regex to avoid "dirty" web links

If this is my actual URL to a file:
http://www.example.org/posts.php?post=example-post-name
In my .htaccess file, how can I use a regular expression to get to this path when a user submits:
http://www.example.org/posts/example-post-name
So far I've come up with this bringing together a few examples (this also included a www redirect):
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) /$1.php [L]
RewriteRule ^posts/([A-Za-z])/$ /posts.php?post=$1
But I'm not having much luck with it, can anyone tell me where I'm going wrong?
You need a + after your A-Za-z group to indicate one or more characters, and also you need to add a - to the end of that group. At the end, the /? indicates that the final slash may or may not be present.
Finally, add [L] to be sure no further rewrite rules get processed.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*) http://%1/$1 [R=301,L]
# First rewrite the posts:
RewriteRule ^posts/([A-Za-z-]+)/?$ /posts.php?post=$1 [L]
# ing0 edit: add in dirs that need changing back.
# (I dont know if there is an easier way to do this).
RewriteRule ^posts/css/(.*)$ /css/$1 [L]
RewriteRule ^posts/img/(.*)$ /img/$1 [L]
# etc
# Then, if it's not a real file and doesn't already end in .php
# Note change here ...
RewriteCond %{REQUEST_URI} !\.php$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# redirect it to PHP.
RewriteRule (.*) /$1.php [L]
I think you need to match the whole url and the regex wasn't quite right. Try this:
RewriteRule ^(.*)/posts/([\w-]+)$ $1/posts.php?post=$2
If it works only matching the non-base part of the url, this this:
RewriteRule ^posts/([\w-]+)$ posts.php?post=$1
Why not use:
RewriteRule ^posts/(.*)/$ posts.php?post=$1