.htaccess route all except if contains word - php

So at the moment, any missing pages (404) get redirected to domain.com/search.php and carries the query parameters
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ search.php [QSA,L]
This works great, however, I have an edge case for domain.com/submit where I need it to redirect to domain.com/submit.php
Can't seem to figure out how to do it.
Thanks in advance

You can use this :
RewriteEngine On
RewriteBase /
#rewrite /submit to /submit.php
RewriteRule ^submit/?$ submit.php [L,NC]
#rewrite other non-existent URIs to /search.pho
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ search.php [QSA,L]

Like this, I suggest adding NC so it's case-insensitive:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^submit?$ submit.php [NC, L]
RewriteRule ^(.+)$ search.php [NC, QSA,L]

Related

htaccess rewrite rule for subfolder (nice url)

I have trying to figure out how to get working in subfolder "admin" the same thing which works in root dir, problem is I don't know how to do that.
Currently I have:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
# /
RewriteCond %{REQUEST_URI} !\.[[:alnum:]]+$
RewriteRule ^(.+[^/])$ /$1/ [R=301]
Now it works like:
http://example.com/page translate into http://example.com/?arg=page
What I would like to do in addition is this:
http://example.com/admin/page translate into http://example.com/admin/?arg=page
I don't know at all how to make that happen, can somebody help me please?
Thanks
You can use these rules with appropriate RewriteBase:
RewriteEngine On
RewriteBase /admin/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ?arg=$1 [L,QSA]
Or just add
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/admin/(.*)$ admin/index.php?arg=$1 [L,QSA]

.htaccess redirection based on number of slashes(/) in url

This is my first htaccess file.
this is what i have written
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
The problem what I am facing is both are pointing to maincategory.
How can I redirect based on number of "/" in the URL?
Thanks in advance.
Rearrange the rules and try it this way. Let me know if that works for you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]

htaccess is not redirecting

Well, i want the user to enter localhost/$value so it will redirect to localhost/pirsum/index.php?page=g&link=$value but its not working.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} ^/([^/]+)/?$ [NC]
RewriteRule .* index.php?page=g&link=%1 [L]
I tried to enter localhost/ AND localhost/pirsum and both ended on 404 Not found site.
Now, thats the only part of my website that i need to use htaccess, so i dont want the whole site to redirect, just that part.
Use this rule:
RewriteEngine On
RewriteBase /pirsum/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=g&link=$1 [L,QSA]

Modification to a RewriteRule

The following RewriteRule works correctly
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
until I place this one just right below
RewriteRule ^([a-z0-9]+)/$ /profile/company-profile.php?cname=$1 [NC,L]
Now every domain.com/something-here goes to the company-profile.php
How can I fix this ?
Firstly, the domain.com/login takes you to the index.php of the login folder.
When I type the second RewriteRule, if I write domain.com/login again it shows (in the background) the company-profile.php?cname=login
I would recommend to use this:
RewriteEngine On
RewriteBase /
# redirect company specific request
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z0-9\-_]+)/?$ profile/company-profile.php?cname=$1 [NC,L]
# redirect all others
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{REQUEST_URI} !/?$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]

Small error when using .htaccess for clean urls

I am using .htaccess to manage clean URL's but for some reason I get a 404 Not Found Apache error in some cases.
If a user goes to http://domain.com/profile/ everything is fine.
If a user goes to http://domain.com/profile they will get the error
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ /index.php?page=$1&cmd=$2 [L]
Try this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
# /profile/cmd
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /index.php?page=$1&cmd=$2 [L]
# /profile
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)$ /index.php?page=$1 [L]
</IfModule>
You can redirect the url with the url which has trailing slash using 301 redirect
Try the following along with your rewrite condition. This will redirect any url request to url with trailing slash. ex. http://domain.com/profile will be redirected to http://domain.com/profile/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !example.php
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://domain.com/$1/ [L,R=301]
The rewrite rule you wrote requires one slash '/'.
Try this instead:
RewriteRule ^([^/]*)(/([^/]*))?$ /index.php?page=$1&cmd=$3 [L]

Categories