Htaccess rewrite rules method get - regex

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]

Related

A special rule for a directory in .htaccess

I have this .htaccess file:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [QSA]
I'd like to create an exception of the last rule: if the user enters example.com/somefoldername It should not redirect to index.php?p=somefoldername, but to ./somefoldername/index.php.
It is a very easy task I guess! But can't come up with a solution. I tried
RewriteCond ^somefoldername$ somefoldername/index.php but it didn't work.
Thanks in advance!
You can use:
RewriteEngine on
RewriteBase /
RewriteRule ^(somefoldername)/?$ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?p=$1 [L,QSA]

hide folder using .htaccess with subfolders

I have been trying to figure out this for a while but no success-
I have this site structure http://example.com/catalog/current/sub-folders/..
The result should hide the folder "current" so that the paths look like http://example.com/catalog/sub-folders/
This is what I have so far-
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+current/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^current/)^(.*)$ /current/$1 [L,NC]
when I place this .htaccess to the root and go to http://example.com/catalog/sub-folders/, it try to look for /current/catalog/sub-folders/
Any help to approach this problem will be highly appreciated.
Keep your code like this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/+(catalog)/current/(\S*) [NC]
RewriteRule ^ %1/%2 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(catalog)/((?!current/).*)$ $1/current/$2 [L,NC]
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^(.+)$ $1.php [L]
Try making the last rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^hsc-bulletin/)^(.*)$ /current/$1 [L,NC]
This instead:
RewriteCond %{REQUEST_URI} !^/current/
RewriteRule ^(?!hsc-bulletin/)(.*)$ /current/$1 [L,NC]

Htaccess clean user profile url is not redirecting

I cannot get the clean user profile url to work. I already tried everything!
I want to redirect
http://localhost/test/profile?username=gadgetster
to
http://localhost/test/profile/gadgetster
Here is my attempt:
Options -Indexes -MultiViews +FollowSymlinks
RewriteEngine On
RewriteBase /test/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA]
why isn't this working?
Have it like this:
Options -Indexes -MultiViews +FollowSymlinks
RewriteEngine On
RewriteBase /test/
RewriteCond %{THE_REQUEST} /profile\.php\?username=([^\s&]+) [NC]
RewriteRule ^ profile/%1? [R=302,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^profile/(.+)$ profile.php?username=$1 [L,NC,QSA]
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Make sure there is no .htaccess in /test/profile/.

Redirecting /page.html to /page doesn't work inside subdirectory

I'm trying to redirect from /page.html to /page inside of a subdirectory, but it doesn't work.
What I do: http://example.com/subdirectory/page.html
What I get: http://example.com/page
What I want: http://example.com/subdirectory/page
.htacces file:
RewriteEngine On
#example.com/page will display the contents of example.com/page.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+)$ $1.html [L,QSA]
#301 from example.com/page.html to example.com/page
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule ^(.*)\.html$ /$1 [R=301,L]
How can I get this to work?
Use this code for .html hiding in your DOCUMENT_ROOT/.htaccess file:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+(.+?)\.html[\s?] [NC]
RewriteRule ^ /%1 [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ /$1.html [L]
Try this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]
</IfModule>
Thanks

Redirecting site to sub-sub folder

I have a website in my public_html folder placed in the following folder structure
public_html/dps/main
I am using .htaccess to redirect in the following format ;
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond /domain/%{REQUEST_URI} -d
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /dps/main/$1/
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteCond %{REQUEST_URI} !^/dps/main/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /dps/main/$1
RewriteCond %{HTTP_HOST} ^(www.)?website.co.uk$
RewriteRule ^(/)?$ /dps/main/index.php [L]
Unfortunately it gives me Internal Server Error
not sure what is wrong with this code.
Thank you for your help in advance
Replace your rules with this:
DirectoryIndex index.php
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?website\.co\.uk$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^((?!dps/main/).*)$ /dps/main/$1 [L,NC]