Apache htaccess rule for url shortening - apache

I'm trying to create something
short.com/ASWi7 -> short.com/index.php?h=ASWi7
so I tried using this code on my htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1 [L]
but the website loses the css and everything...i organized my site in folder etc...
so how can i exclude all the folders or cetain files to be part of that rule?

Try the following:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule ^(.*)$ /index.php?hash=$1 [L]
!-f checks for existing files
!-d checks for existing directories
Hope that helps.

Related

htacess RewriteRule http://domain/folder/(any searchstring) to redirect to http://domain/folder/index.php?option=$1

I am trying to create a rewrite rule in my Apache .htaccess file and the goal is:
http://domain/folder/(any search string) to redirect to http://domain/folder/index.php?option=$1
example:
http://domain/folder/covid to redirect to http://domain/folder/index.php?option=covid
I got this setup in htacess in http://domain/folder, but somehow it's not working:
RewriteEngine On
RewriteRule ^([^/folder]+)/([^/]+)$ http://domain/folder/index.php?option=$1 [L]
The website is hosted on http://domain/folder
Can someone help me on this?
You may try this .htaccess inside folder/:
RewriteEngine On
RewriteRule ^index\.html?$ index.php [L,NC]
RewriteRule ^index\.php$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .+ index.php?option=$0 [L,QSA]
You could have your htaccess file like as follows. Please keep your htaccess file along side with your folder(named folder) and NOT inside folder.
Please make sure you clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?option=$1 [L]
OR in case your htaccess is present inside folder folder then try following.
RewriteEngine ON
RewriteBase /folder/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?option=$1 [L]

Rewrite urls in htaccess file - remove query string

I have never edited the .htaccess file before so sorry for my awful attempt
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/index\.php$
RewriteCond %{QUERY_STRING} ^id=([0-9]*)$
RewriteRule ^(.*)$ http://example.site/index/%1 [R=302,L]
I am tryting to rewrite http://ex.com/shop/?s=sa to look like http://ex.com/shop/sa
I am assuming you are using rewriterules in shop directory and index.php is handling the data then try with below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)$ index.php?s=$1 [L]

My Apache rewrite code (provided) in .htaccess works, but also re-writes favicon.ico. How fix?

I have always used this simple code to have all requests use one index.php file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .? index.php [L]
However, I have just found that it's also re-writing the favicon.ico file.
My understanding is that the code above should rewrite only index.php and no other file or directory. Am I wrong? How would I fix this?
Just add an exception for it in your rules.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/favicon.ico [NC]
RewriteRule .? index.php [L]

Use specific PHP file for specific directories

I am trying to create mod_rewrite rules that rewrite based on the first level directory name plus a failover to rewrite to a standard file in case none of the directory names are matched.
Example:
I have units.php, models.php and other.php. The other.php file should handle all non-assigned requests.
http://www.mydomain.com/units/4435
Should redirect to /units.php?id=4435
http://www.mydomain.com/models/594
Should redirect to /models.php?id=594
http://www.mydomain.com/anything
Should redirect to /other.php?id=anything
http://www.mydomain.com/anything/893
Should redirect to /other.php?id=anything/893
Let me know if this makes sense. I am unsure of how to structure the rules and conditions to achieve what I want.
This is what I have tried to sfar. It works for URLs starting with 'units' or 'models' but I get a 500 Error if I try any other URL:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(units)/(.+)$ /units.php?id=$2 [L,NC]
RewriteRule ^(models)/(.+)$ /models.php?id=$2 [L,NC]
RewriteRule ^(.+)$ /other.php?id=$2 [L,NC]
I would suggest this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(units|models)/([0-9]+)/?$ /$1.php?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(units|models)/[0-9]+/?$
RewriteRule ^/[^/]+/([0-9]+)/?$ /other.php?id=$1 [L,QSA]

CodeIgniter htaccess subfolder problem

I want a second website in my domain inside the folder test
www.mydomian.com/test
Apache server running on linux.
But when I load it in my navigator styles, images, helpers can't be found.
My htaccess is like this:
RewriteEngine On
RewriteBase /test
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Thank you in advance
I have tried what you said, placing the htaccess file you gave me in the test folder and the other one in the root directory, it didn't work.
Other options?
Try adding a .htaccess file into your test folder instead.
I use this .htaccess content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
In the folder test I would place another .htaccess, with almost the same content:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/index.php?/$1 [L]