I want that when domain.ext/sitemap.xml is called, domain.ext/sitemap.xml.php is opened. This I got working.
My problem is that the browser redirects to the php file, I dont want that, it should keep saying sitemap.xml
# Catch sitemap
RewriteCond %{REQUEST_URI} ^/sitemap.xml$
#RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) http://%{HTTP_HOST}/sitemap.xml.php [L]
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule ^(.*)/? index.php?q=$1 [L]
Anyone who can tell me why?
Remove http:// from target URI to enable internal rewriting:
# Catch sitemap
RewriteRule ^sitemap\.xml$ /sitemap.xml.php [L]
## Internally rewrite extensionless file requests to .php files ##
# If the requested URI does not contain a period in the final path-part
RewriteCond %{REQUEST_URI} !(\.[^./]+)$
# test if sitemap
RewriteCond %{REQUEST_URI} !^/sitemap
# and if it does not exist as a directory
RewriteCond %{REQUEST_FILENAME} !-d
# and if it does not exist as a file
RewriteCond %{REQUEST_FILENAME} !-f
# then add .php to get the actual filename
RewriteRule ^(.+?)/?$ index.php?q=$1 [L]
PS: If target URI starts with http:// then mod_rewrite redirects with R=302 status.
Related
When a page is visited using a URL that ends with .html, I'd like the URL to change to having no extension and report 301 permanently redirected. I'm having serious difficulty. After reading a lot of documentation and tutorials, and searching Stack Overflow for hours, the closest I've achieved is the opposite (URLs with no extension having one added) with this code:
<Location />
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_URI} !\.html$
RewriteRule ^(.*)$ https://%{HTTP:Host}%{REQUEST_URI}.html [L,R=permanent]
</Location>
This can be done in two steps, with a REDIRECT_LOOP environment variable to prevent looping, with the directives in a Directory section for the web root (or .htaccess in the web root) so the matched string in the RewriteRule can be used for the permanent Redirect.
<Directory "/var/www/html">
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d # if the request is not a directory
RewriteCond %{REQUEST_FILENAME} !-f # if the request is not a file
RewriteCond %{REQUEST_FILENAME}\.html -f # if adding .html it is a file
RewriteCond %{REQUEST_URI} !\.html$ # if the request doesn't end in .html
RewriteCond %{REQUEST_URI} !/$ # if the request doesn't end in /
RewriteRule ^(.*)$ $1.html [L,E=LOOP:1] # then return the request as if it ended with .html and set the loop variable
RewriteCond %{ENV:REDIRECT_LOOP} !1 # if we didn't just added .html
RewriteRule ^(.*)\.html$ https://%{HTTP:Host}/$1 [L,R=permanent] # then 301 redirect the request to the request without the .html
</Directory>
This will make it so that if you have example.html and example/index.html then example.html will never be loaded.
We have been using the following .htaccess file for years. We are using Apache 2.4.7. And for some reason, static files seem to be hitting our front controller index.php!
For example: https://example.com/apple-touch-icon.png is being handled by our front controller as we can see the response header includes X-Powered-By:PHP/5.5.9-1ubuntu4.3.
Can anyone spot the issue?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Force SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
It appears that your ErrorDocument directive is causing this (loading index.php when some resource i.e. image, js or css file is not found.
You can comment out this directive to avoid this behavior:
ErrorDocument 404 /index.php
I'm working on an add-on (for a CMS) to include news. To prevent creating many index-files I tried to use redirection with htaccess, but it seems to be complex for me ;-)
I'm using a .htaccess for the CMS in the root directory:
ErrorDocument 404 404.php
RewriteEngine On
# Redirect from http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} !^domain.tld$ [NC]
RewriteRule ^(.*)$ https://domain.tld/$1 [L,R=301]
# If called directly - redirect to short url version
RewriteCond %{REQUEST_URI} !/page/intro.php
RewriteCond %{REQUEST_URI} /page
RewriteRule ^page/(.*)$ /$1 [L,R=301]
# Send the request to the index.php for processing
RewriteCond %{REQUEST_URI} !^/(page|backend|framework|include|languages|media|account|search|temp|templates/.*)$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\/\sa-zA-Z0-9._-]+)$ /index.php?$1 [QSA,L]
# allow robots.txt (all other txt are denied before)
RewriteCond %{REQUEST_URI} !/robots\.txt$ [nocase]
RewriteRule \.txt$ - [forbidden,last]
The current structure:
--> page/
----> folder1/accessfile.php
----> folder1/.htaccess
I'd like to redirect from:
/page/folder1/accessfile/lorem/ipsum
and
/page/folder1/accessfile/lorem/ipsum/ #(folders that doesn't exist)
to:
/page/folder1/accessfile.php
I'd tried using this in page/folder1/.htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^accessfile/.*$ ./accessfile.php
But this doesn't work :-(
Put the htaccess file in your root directory alongside the page/ directory. If your htaccess file already resides there, put the rewrite rules before the # If called directly - redirect to short url version line.
Here's what you need:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Make sure it's not an actula file/dir being accessed
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Make sure request uri does not contain the actual file
# name, avoiding recursive rewrite loops
RewriteCond %{REQUEST_URI} !accessfile\.php
RewriteRule page/folder1/accessfile/?(.*)? /page/folder1/accessfile.php?q=$1 [R,L,QSA]
</IfModule>
## Results
# page/folder1/accessfile => page/folder1/accessfile.php?q=
# page/folder1/accessfile/foo => page/folder1/accessfile.php?q=foo
# page/folder1/accessfile/foo/bar => page/folder1/accessfile.php?q=foo/bar
# page/folder1/accessfile/foo/bar/baz => page/folder1/accessfile.php?q=foo/bar/baz
As you see, for convenience, the rewrite rule rewrites the request URI as query string parameters. So the PHP file has access to the passed data using $_GET['q'].
If you wish to preserve the clean URL, and rewrite the request to the php file under the hood, drop that R flag.
First off, I know there are many questions similar to this one. I've read everything I can find, but the solutions I see elsewhere don't seem to work for me. I'm really hoping someone can give me some insight here.
I am trying to use Apache's .htaccess directives to force specific pages on my server to use ssl. In addition to those directives, I'm also using some rewrites to mask .php and .html extensions.
I created a page, https-test.html. I want that page specifically to always get redirected so it uses https and so that .html gets stripped off, like https://www.example.com/https-test
However, I seem to always end up with a loop. Reading the Apache docs for 6 hours got me closer, but I'm still missing something.
Below is my annotated htaccess file.
RewriteEngine on
# If port is insecure...
RewriteCond %{SERVER_PORT} ^80$
# And requested URI is /https-test...
RewriteCond %{REQUEST_URI} ^(.*/)https-test$ [NC]
# Then point the server to the secure url:
RewriteRule . "https://www.example.com/https-test" [L,R]
# The next few lines try matching extensionless requests to .php files
# If the requested file is not a directory...
RewriteCond %{REQUEST_FILENAME} !-d
# And we CAN find a .php file matching that name...
RewriteCond %{REQUEST_FILENAME}\.php -f
# Then point us to that .php file and append the query string.
RewriteRule ^(.+)$ $1.php [L,QSA]
# These next few lines were added by the previous project owner
# They're supposed to redirect requests like /foo.html to /foo,
# But I suspect these might be the culprit
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)/$ /$1 [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /$1.html [NE,L]
# Next few lines are legacy SEO stuff, some pages were linked to as
# php but now are html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} .php$
RewriteRule ^(.*).php$ /$1.html [L,NE]
So that's the code I have in my htaccess. And if I go to http://www.example.com/https-test in Chrome, I get www.mysite.com redirected you too many times.
You should probably just rewrite the code a bit. You are trying to match both extensionless files to php and html and doesn't look like you're accounting for each of the conditions. You should add a condition to make sure they are not tryiing to do the same things.
Backup your code, replace your code with this and give it a try. Clear all your cache before trying.
RewriteEngine on
# If port is insecure... redirect for a specific page
RewriteCond %{HTTPS} !^on [OR]
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule ^http-test/?$ https://www.example.com%{REQUEST_URI} [R=301,L]
# Next few lines are legacy SEO stuff, some pages were linked to as
# php but now are html
RewriteCond %{THE_REQUEST} ^GET\ /(.+)\.php
RewriteRule ^ /%1? [R=301,L]
# The next few lines try matching extensionless requests to .php files
# If the requested file is not a directory and php file exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)$ $1.php [L,QSA]
#remove trailing slash and is not a php file
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule ^([^\.]+)/$ /$1 [R=301,NE,L]
#finally redirect extensionless URI to html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ /$1.html [NE,L]
Note I haven't tested this fully.
I'm working on an API system with PHP, but I don't want the URLs to contain the file extension. Therefore, I've created a .htaccess file containing the following:
RewriteEngine On
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://xxxxx.com/api/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://xxxxx.com/api/$1 [R=301,L]
# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]
When opening http://xxxxx.com/api/some_call.php, it redirects me to http://xxxxx.com/api/some_call, but I get a 404 not found error:
The requested URL /api/some_call was not found on this server.
I have full control over the (root) server, so any suggestions are welcome.
Thanks in advance,
CrushedPixel
Try adding the conditions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)$ $1.php [L]
And try and add Options -Multiviews above the RewriteEngine On