Remove .html from URL and rewrite requests to index.html - regex

I'm not very experienced with .htaccess related stuff, I was looking to get some help. Basically what I'm trying to do is this:
Any request that looks like this (example):
/foo.html
Would be rewrote to:
/foo
And any request that is a static file, I'd like it serve:
/index.html
Does that make sense? Any idea how to do this?
Example
Here's what I have so far, though it's not correct as far as I know:
RewriteRule %{REQUEST_FILENAME} !-d
RewriteRule %{REQUEST_FILENAME} -f
RewriteCond ^(.*)$ $1.html [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*) /index.html [NC,L]

Check for existence of .html file before adding it to URIs:
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule ^(.*)$ $1.html [L]
RewriteRule . /index.html [L]

Related

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

Htaccess rewrite rules method get

Hi i want rewrite:
example.com/articles to example.com/?rt=articles
and
example.com/articles/some-title-here to example.com/?rt=articles&title=some-title-here
My code:
RewriteEngine on
Options -Indexes
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]
RewriteRule ^articles/(.*)$ index.php?rt=articles&title=$1 [L,QSA]
First rewrite rule it's working but second doesn't work...
Have it this way:
RewriteEngine on
Options -Indexes
# ignore all files and directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^articles/(.+)$ index.php?rt=articles&title=$1 [NC,L,QSA]
RewriteRule ^(.*)$ index.php?rt=$1 [L,QSA]

wildcard .htaccess and reject some address on subdomain

I want all file or directory existed under up.example.com subdomain rejected and show 404 error.
Also I want all another request point to index.php.
I write this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [R=404,L,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
When I test this code for http://up.example.com/test.jpg
If test.jpg file existed on root server, show me this error:
Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80
It's ok!
But when test.jpg not existed, give me this error:
Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80
Instead open index.php (index.php exist on root)
Why? and how can I fix it?
Actually the problem is that you write everything to index.php and that becomes a valid file in the request. When mod_rewrite runs next time first rule sends it to 404.
To overcome this error have your rules like this:
RewriteEngine On
# if it is valid file or directory except for index.php
# then send 404 to browser
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^index\.php$ - [R=404,L,NC]
# send all non-file/directories to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
Try this one
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC] # remove if it is not related to subdomain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ - [R=404,L,NS]

Removing url file extension in .htaccess

I know this is a pretty common task and has been discussed here a lot, but I still can't get it to work. After doing some research I added this code into my .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
But it still doesn't work and displays the .html on my pages.
What could be wrong?
Thanks
You need an additional rule to externally redirect foo.html to foo. Have your .htaccess like this:
RewriteEngine on
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]

Make nice URL for calendar

I am trying to create a nice looking url for my calendar site.
But I can't get it to work properly.
This is my URL:
http://mydomain.com/admin/calendar?year=2013&month=november
...and I want it to look like this:
http://mydomain.com/admin/calendar/2013/november
The "calender"-folder is not a real folder and the .htaccess file is located in the admin-folder and not in the root of my website.
I am currently using this code:
RewriteEngine On
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .+ - [L]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
# Make new URL
RewriteBase /admin/
RewriteRule ^([^/]*)/([^/]*)$ /admin/calendar/calendar?year=$1&month=$2 [L]
Any idea why this doesn't work, and how to get it to work? :D
Thanks :)
TheYaXxE
Try this code in your admin/.htaccess file:
RewriteEngine On
RewriteBase /admin/
# Make sure not to rewrite real files
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# Make new URL
RewriteRule ^calendar/([^/]+)/([^/]*)/?$ calendar.php?year=$1&month=$2 [NC,L,QSA]
# Removes PHP extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+?)/?$ $1.php [L]
Try this:
RewriteRule ^([^/]+)/([^/]+)/$ /admin/calendar/calendar?year=$1&month=$2