I'm trying to remove the .html file extension from my URL's to make them look nicer. I've seen many examples of this and have tried them, but I'm struggling to find something that only works in the root, and doesn't apply to any subdirectories or subdomains.
Can I get any help with this?
Example:
example.org/test.html > example.org/test
example.org/food/xyz.html > example.org/food/xyz.html
login.example.org/something.html > login.example.org/something.html
Your question is a bit vague, so I will concentrate on the main issue you mention:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !/.+\.html
RewriteRule ^/?([^/]+)$ $1.html [L]
Alternative approach:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ $1.html [L]
If this is limited to only a single host name (as your examples might suggest) depends on your setup. Such thing is easiest done if you place the rule in the real host configuration of your http server instead of using .htaccess style files. That is also why I append a general hint:
You should always prefer to place such rules inside the http servers host configuration instead of using .htaccess style files. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
In the comment you wrote that you also want to force the shorter notation, so redirect users request a URL with such "file name extension" to the version without.
I don't really see any point in this, but it certainly is possible:
RewriteEngine on
RewriteCond %{ENV:REDIRECT_STATUS} !^.
RewriteRule ^. - [L]
RewriteCond %{REQUEST_URI} ^/.+\.html$
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?(.+)\.html$ /$1 [L,R=301]
RewriteCond %{REQUEST_URI} !^/.+\.html$
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?([^/]+)$ /$1.html [L]
Related
My greetings to all.
I have a problem for which I am looking for a solution.
I have an my-website.com site and every user who registers on my site gets a personal link: USERNAME.my-website.com .
Meanwhile, I had the following rule in my htaccess :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule (.*) process.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php/$1 [L]
How to make all sub-domains ( USERNAME.my-website.com ) point to process.php ( process.php/USERNAME ) file and that the others urls like my-website.com/OTHER point to index.php/USERNAME ? ( in case OTHER is not a file or a folder of my site )
Here's what I did, but it doesn't work.
"But it doesn't work" is a bit vague, so all we can do is given some general advice and suggestions...
First you need to make sure that those subdomains are actually served at all by your http server. You did not write anything about that, nor how you host your site at all.
Also you obviously have to take care that distributed configuration files (".htaccess") are considered at all by your http server, also something which is not clear from your question.
Given both of those aspects you will indeed have to concentrate on your rewriting rules. Here is a variant of your own implementation with a few changes:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/process\.php$
RewriteCond %{HTTP_HOST} ^(.*)\.my-website\.com [NC]
RewriteRule ^ /process.php/%1 [QSA,END]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !^/index\.php/?
RewriteRule ^/?(.*)$ /index.php/$1 [QSA,END]
You probably will have to make some more changes. But based on the vague information you gave it is hard to be more specific.
A good way to start debugging issues with rewriting is to enable rewrite logging inside your http server. That allows to closely monitor what is actually happening inside your rewriting engine. Please consult the documentation about that: https://httpd.apache.org/docs/current/mod/mod_rewrite.html
I'm using this .htaccess to redirect each subdomain to a folder named the same as subdomain and all requests to corresponding index.php.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteCond %{REQUEST_URI} !^/sub/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /sub/$1
RewriteCond %{HTTP_HOST} ^(www.)?sub.domain.com$
RewriteRule ^(/)?$ sub/index.php [L]
It works great, but I'd like it to work with multiple subdomains.
Based on several answers I found in SO, the code should look somewhat like the below, but I can't get it to work.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteCond %{REQUEST_URI} !^/(.*)/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /%1/$1
RewriteCond %{HTTP_HOST} ^(www.)?(.*).domain.com$
RewriteRule ^(/)?$ %1/index.php [L]
Something like this should do wildcard redirect, unless the domain name begins with www.
Note the negative condition using the exclamation mark.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteCond %{HTTP_HOST} ^(.+?)\.domain\.com$ [NC]
RewriteRule ^ http://example.com/%1/index.php [R=301,L]
^(.+?) captures whatever precedes domain.com, unless it is www.
PS: sorry for the last line, it should read domain.com (whatever your actual domain is) instead of example.com. I had to to do this because of posting restrictions on SO.
This should be the straight forward approach:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.*)\.example\.com$
RewriteRule ^ /%1%{REQUEST_URI} [QSA,END]
If will redirect all host names within the domain "example.com" and drop that historic "www." prefix people are always so worried about. This does not handle the main domain (with or without "www." prefix), you'd have to add that if you do not operate a separate virtual host for that special one. The index.php stuff should better be handled using the DirectoryIndex directive the apache http server offers. This leads to less complex setups and cleaner URLs.
In case you receive an internal server error (http status 500) using the rule above then chances are that you operate a very old version of the apache http server. You will see a definite hint to an unsupported [END] flag in your http servers error log file in that case. You can either try to upgrade or use the older [L] flag, it probably will work the same in this situation, though that depends a bit on your setup, certainly you will need to add another rewriting condition to break an endless rewriting loop.
This implementation will work likewise in the http servers host configuration or inside a distributed configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a distributed configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using distributed configuration files (".htaccess"). Those distributed configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
After some changes on my wordpress site, ther are many links out there in the web which cant be accessed anymore. The old urls look like
http://example.com/blog/2017/05/post ans should now show this url
http://example.com/post
I tried this code in .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/([^/]+)/([^/]+)/?$ $1 [R=301,L]
This doesnt work. Than tried this one.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^blog/././?$ $1 [R=301,L]
This also doesnt work.
Most likely this is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/?blog/([^/]+)/([^/]+)/(.+)/?$ $1 [R=301,L]
And a general hint: you should always prefer to place such rules inside the http servers (virtual) host configuration instead of using dynamic configuration files (.htaccess style files). Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
I'm somewhat new to htaccess rewrite rules, and have been scratching my head for the past few days on what's happening here. No amount of Googling seemed to help, so hopefully somebody knows the answer.
I have a site that can be accessed as:
www.site.com
www.site.com/684
www.site.com/684/some-slug-name-here
All of these scenarios should go to index.php and pass in the optional id=684 and slug=some-slug-name-here
Which works fine.
My problem is I have a separate file. Right now it's called admintagger.php - but this fails when I call it anything. 21g12fjhg2349yf234f.php has the same issue.
The problem is that that I would like to be able to access admintagger.php from www.site.com/admintagger
but it seems to be matching my rule for index, and taking me there instead.
Here is my code:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^imagetagger$ /imagetagger.php [NC,QSA]
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
If you want to arbitrarily be able to access php files via the name (sans extension) then you need to create a general rule for it. But you need to be careful otherwise you may be rewriting legitimate requests for existing resources (like a directory, or a slug). Try this instead:
# make sure we aren't clobbering legit requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# see if appending a ".php" to the end of the request will map to an existing file
RewriteCond %{REQUEST_FILENAME}.php -f
# internally rewrite to include the .php
RewriteRule ^(.*)$ /$1.php [L]
Then you can have your routing to index.php right after that:
RewriteRule ^([0-9]+)/?(.*)?/?$ index.php?id=$1&slug=$2 [NC,L,QSA]
Although you may be better off create a separate rule for each of your 3 cases:
RewriteRule ^([0-9]+)/([^/]+)/?$ /index.php?id=$1&slug=$2 [NC,L,QSA]
RewriteRule ^([0-9]+)/?$ /index.php?id=$1 [NC,L,QSA]
RewriteRule ^$ /index.php [L]
I have following rewrite rules for a website:
RewriteEngine On
# Stop reading config files
RewriteCond %{REQUEST_FILENAME} .*/web.config$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .*/\.htaccess$ [NC]
RewriteRule ^(.+)$ - [F]
# Rewrite to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(/bilder_losning/|/bilder/|/gfx/|/js/|/css/|/doc/).*
RewriteRule ^(.+)$ index.cfm?smartLinkKey=%{REQUEST_URI} [L]
Now I have to exclude a script including its eventually querystrings from the above rules, so that I can access and execute it on the normal way, at the moment the whole url is being ignored and forwarded to the index page.
I need to have access to the script shoplink.cfm in the root which takes variables tduid and url (shoplink.cfm?tduid=1&url=)
I have tried to resolve it using this:
# maybe?:
RewriteRule !(^/shoplink.cfm [QSA]
but to be honest, I have not much of a clue of urlrewriting and have no idea what I am supposed to write. I just know that above will generate a nice 500 error.
I have been looking around a lot on stackoverflow and other websites on the same subject, but all I see is people trying to exclude directories, not files. In the worst case I could add the script to a seperate directory and exclude the directory from the rewriterules, but rather not since the script should really remain in the root.
Just also tried:
RewriteRule ^/shoplink.cfm$ $0 [L]
but that didn't do anything either.
Anyone who can help me out on this subject?
Thanks in advance.
Steven Esser
ColdFusion programmer
Please try to put the following line at the top of your config (after RewriteEngine on):
RewriteRule ^shoplink.cfm$ - [L]