htaccess remove param key not working as intended - .htaccess

I currently have in place a rewrite rule to remove a key on one of the sites.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
I now want to add this rule.
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
However, this does not work.
HOST_NAME/stash/123 will not work.
HOST_NAME/stash?sid=123 will.
The PHP is simple....
echo $_GET['sid'];
Here is my full HTACCESS
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.com
RewriteRule ^(.*)$ https://www.hostname.com/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /stash/index.php?sid=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /results/index.php?/$1 [L]
Any help on this would be great.
UPDATE
There are four main directories on the website.
/results
/i
/stash
/account
I dont care if /results shows ?id=1$store=1...etc
I dont want /i and /stash to show that. I would want those to look like...
/i/123456789 instead of /i?id=123456789
/stash/hgdG54Hj9 instead of /stash?sid=hgdG54Hj9

Looks like you have nice directory based URI structures. In that case you can just have a per directory .htaccess
You may use this code in your root .htaccess:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(?:www\.)?(hostname\.com)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Inside stash/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]
And similarly inside i/.htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?id=$1 [L,QSA]

Related

htaccess two endpoints showing same

I have two endpoints on my website /i/ snd /a/. They do show different information, but both the same. Currently, I have the /i/ working like so..
/i/12345 (where 12345 is the itemId).
/a/67890 (where 67890 is the itemId that is active).
If I goto /a/67890, itll show the /i/ UI, not the /a/ UI. Here is my .htaaccess
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.app
RewriteRule ^(.*)$ https://www.hostname.app/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /i/index.php?id=$1 [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /results/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /a/index.php?id=$1 [NC,L]
What am I doing wrong? It looks like /a/ is being routed to /i/.
Have it this way:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{HTTP_HOST} ^(www\.)?hostname\.app$ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,NE,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([ia])/(.*)$ $1/index.php?id=$2 [NC,QSA,L]
RewriteRule ^(results)/(.*)$ $1/index.php?/$2 [L,QSA]

htaccess combine folder with alias

I have 3 folders in a sub directory of my root directory i would like to combine.
My site map:
mywebsite.com
mywebsite.com/test/
mywebsite.com/test/one/
mywebsite.com/test/one/one.php
mywebsite.com/test/two/two.php
mywebsite.com/test/three/three.php
I would like to access:
mywebsite.com/test/one/one.php
mywebsite.com/test/two/two.php
mywebsite.com/test/three/three.php
by using:
mywebsite.com/test/combine/one.php
mywebsite.com/test/combine/two.php
mywebsite.com/test/combine/three.php
Solution...
RewriteEngine On
RewriteRule .* - [E=directory:/test/]
RewriteRule .* - [E=one:one/]
RewriteRule .* - [E=two:two/]
RewriteRule .* - [E=three:three/]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:one}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:one}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:one}$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:two}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:two}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:two}$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:three}$1 -f [OR]
RewriteCond %{DOCUMENT_ROOT}%{ENV:directory}%{ENV:three}$1 -d
RewriteRule ^combine/(.*)$ %{ENV:directory}%{ENV:three}$1 [L]
This works but i would like to minimize the script
Try it this way for consolidation, if you simplify it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteCond %{DOCUMENT_ROOT}/test/$1/$1.php -f
RewriteRule ^combine/(.*)(?:\.php)$ /test/$1/$1.php [L]

.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]

.htaccess rewriterule not working for single page

I have a couple pages:
http://waleyqiao.com/about/
http://waleyqiao.com/index/
http://waleyqiao.com/contact/
http://waleyqiao.com/blog/
http://waleyqiao.com/portfolio/
The real pages would be waleyqiao.com/about.html
htaccess:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
For some reason, waleyqiao.com/portfolio/ does not seem to work but waleyqiao.com/portfolio.html does...
It's because you are tacking the .html right on the end of the URI:
/portfolio/.html
which obviously doesn't exist. You need to create a grouping without the trailing slash. Try this instead:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.html -f
RewriteRule ^ /%1.html
And likewise for the php extension:
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/(.*?)/?$
RewriteCond %{DOCUMENT_ROOT}/%1\.php -f
RewriteRule (.*) /%1.php [L]

Add another exception to htaccess?

My .htaccess file alows access to real files & folders on the server. For anything else, it will redirect to /index.php, except when the app folder is specified in which case it will redirect to /app/index.php. I want to add a second folder named appbeta which will redirect to /appbeta/index.php. How should I modify my .htaccess to add this additional exception?
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app(.+)$ /app/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
I am not exactly sure what the following rewrite does:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
However, if your current .htaccess is working as you want, I would try the following which puts a rewrite for /appbeta right before the rewrite for /app:
SetEnv APPLICATION_ENV development
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!app)(.+)$ /index.php?u=$1 [NC,QSA,L]
# rewrite /addbeta to /appbeta/index.php ...
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^appbeta(.*)$ /appbeta/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^app(.+)$ /app/index.php?u=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
note: no new question started as mentioned in comments, fix above (change ^appbeta(.+)$ to ^appbeta(.*)$)

Resources