.htaccess how can I implement both "vanity url" and "no extension" - .htaccess

I'm using htaccess from stackoverflow.com/q/8583856 -
RewriteEngine on
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://www.mysite.com/$1 [R=301,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://www.mysite.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule .* - [L]
RewriteRule ^(.*)$ http://www.mysite.com/profile.php?u=$1 [NC]
Everything works great unless I type www.mysite.com into address bar -
returns mysite.com/profile?u=index.html.var
which errors "Unknown column index.html.var in where clause"
Anyone know how to get this to go to mysite.com/index instead?

Change your code to this and let me know how it behaves (after clearing your browser cache):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [R=302,L]
# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ $1 [R=302,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ profile.php?u=$1 [NC,L]
Once you're sure it is working then change R=302 to R=301

try adding a condition to check if the path is empty (i.e. root)
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond $0 ^$
RewriteRule .* - [L]

Related

remove inside url some element via htaccess

Via an htaccess, I want to remove this point inside url : Products/Description
example : https://test.com/Products/Description/logitech/Id-12
I tried this but it does not work.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
below is my complete htaccess tried rules file:
RewriteEngine On
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
# Remove Products/Description ===> not working
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ Products/Description/$1$2 [L]
With your shown samples and efforts, please try following .htaccess rules file. We need to do few changes in it. Make sure that your .htaccess file is residing along with Products folder(not inside it, besides it).
Also make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /Products/Description/
DirectorySlash Off
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]
# Remove Trailing Slashes
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)//+[?\s]
RewriteRule ^ %1 [R=302,L]
RewriteCond %{THE_REQUEST} \s/index\.php\?([^&]*)&([^&]*)&Id=(\d+)\s [NC]
RewriteRule ^ /%1/%2/%3? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(Products)/(Description)/(\d+)/?$ index.php?$1&$2&Id=$3 [QSA,NC,L]
# Reroute to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

HTACCESS Rewrite, remove .php and redirect working for one rule and not the other

I have the following example urls, there are many possible parameter values
http://www.example.com/area-codes.php?code=0000
http://www.example.com/search.php?number=00000000000
Which I wish to rewrite to the following, respectively.
http://www.example.com/area-codes/0000
http://www.example.com/number/00000000000
Currently, the second url is rewriting and redirecting perfectly.
However the top one is causing an Internal server error
Here's my htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} \.(jpg|css|js|gif|png)$ [NC]
RewriteRule ^ - [L]
#unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [R=301,L]
#resolve .php file for extensionless php urls
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]
#redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*[^.#?\ ]+\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(([^/]+/)*[^.]+)\.php /$1 [R=301,L]
RewriteRule ^area-codes/([^/]*)$ /area-codes.php?code=$1 [L,QSA]
RewriteRule ^number/([^/]*)$ /search.php?number=$1 [L,QSA]
I am assuming there's something wrong with my flag?
Commented out these two lines and it's worked.
#resolve .php file for extensionless php urls
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [L]

How to let htaccess distinguish between files and ids

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d # not an existing dir
RewriteCond %{REQUEST_FILENAME} !-f # not an existing file
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f # and page.php exists
# redirect to the physical page
RewriteRule ^(.*)$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^ /serve.php [L]
RewriteRule ^(\w+)$ ./serve.php?id=$1
So in the first part of the code I just turn http into https. My aim is that I can use urls without an .php but in my old code I had the problem that if I did so, it was used as an id for my serve.php page. So if use https://example.com/contact it was like https://example.com/serve.php?id=contact but I want it to work as https://example.com/contact.php but on the other side I want that ids that arent directions or file should still work as ids. My old code was...
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(\w+)$ ./serve.php?id=$1
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
You can use these rules in your site root .htaccess:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]
# skip rules below this for files and directories
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
# rewrite to the physical php page
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# otherwise, redirect to serve.php
RewriteRule ^/?$ serve.php [L]
RewriteRule ^(\w+)/?$ serve.php?id=$1 [L,QSA]

htaccess remove extension from url works but the page doesn't load

I managed to get /about.php to change to /about with
RewriteBase /
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
But now the page doesn't load. Why?
Any help is much appreciated!
UPDATE:
RewriteBase /
RewriteEngine On
# Do the .php removal first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1
# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
RewriteRule ^pages/(.*)$ /$1 [L,NC,R]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
You've gotten as far as to probe the incoming request for .php and redirect the browser without the .php, but you still need another rule to catch the subsequent URI without .php and silently execute it with .php.
RewriteEngine On
# Do the .php removal first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
# And add a catch-all rule to point requests to .php silently
# if the file exists.
#
# Don't interfere with real file requests:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php [L]
Requests like /something for which /something.php doesn't actually exist will still return a 404, but /something where /something.php does exist will silently execute /something.php without changing the browser address bar from /something.
Now, the above will remove .php even if the file doesn't actually exist, so a request to /notexist.php will redirect the user to /notexist which will result in a 404. If you don't want to even bother redirecting those to just return 404 on /notexist.php, you could do the following to make sure it just returns 404 right away:
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
Update: Order matters.
Seeing other rules already in place, these are highly sensitive to the order they're executed in, particularly since you have a catch-all RewriteRule ^(.*)$ /test/profile.php?username=$1
RewriteBase /
RewriteEngine On
# This removes /index.php, do this first
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
# Do the .php removal next
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
# /pages is specific, so do it next
RewriteRule ^pages/(.*)$ /$1 [L,NC,R]
# Only do this if the .php exists
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/?$ $1.php [L]
# Then the more generic one to write username
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/profile.php?username=$1 [L]
I have tested all of these and verified them in my own environment.

.htaccess - Clean URL, 500 Internal Server Error

I've got this as my .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
# Where it's all about
RewriteRule ^users/([a-zA-Z0-9]+)$ users.php?user=$1
RewriteRule ^users/([a-zA-Z0-9]+)/$ users.php?user=$1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.daltonempire.nl [nocase]
RewriteRule ^(.*)
http://daltonempire.nl/$1 [last,redirect=301]
I tried to make clean urls, redirecting daltonempire.nl/users/Me to daltonempire.nl/users.php?user=Me.
However, I failed miserably. My website now constantly returns a 500 Internal Server Error.
(I possibly somehow created a redirection loop?)
What did I do wrong? (And what do I have to do to fix it?)
Believe you have an extra newline in last rule. Also make sure to use L (Last) flag in all of your rules.
RewriteEngine on
# Where it's all about
RewriteRule ^users/([a-zA-Z0-9]+)/?$ users.php?user=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
RewriteCond %{HTTP_HOST} ^www\.(daltonempire\.nl)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [L,R=301,NE]

Resources