How to modrewrite in my subfolder - regex

I have a PHP script on a subfolder of my server and it generates url like this:
http://example.com/subfolder//index.php?a=profile&u=username
Instead I would like a SEO friendly url like:
http://example.com/subfolder/profile/username.html
I'm on Apache and mod_rewrite is enabled, so I edited the .htaccess of my subfolder page:
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
But it doesn't work.
This is my full .htaccess code:
RewriteEngine on
RewriteCond %{request_filename} -f
RewriteRule ^(.*) $1 [L]
RewriteRule ^(([^/]*)+)(/([^/]{0,32})(/.+)?)?$ index.php?a=$1&q=$3 [L]
RewriteRule ^([^/]*)/([^/]*)\.html$ //index.php?a=$1&u=$2 [L]
What's wrong with my code?

Use this rule in /subfolder/.htaccess:
RewriteEngine on
RewriteBase /subfolder/
RewriteRule ^([^/]+)/([^/]+)\.html$ index.php?a=$1&u=$2 [L,QSA,NC]

Related

Apache .htaccess "Redirect to non-url" error

I've not used URL rewriting on .htaccess for a while, and now I think I've forgot some of the basis.
This is my server directory structure:
https://www.domain.com/dir/
In which I've put my website, with those files:
index.php
list.php
edit.php
home.php
What I wanted to achieve is this: User writes url like https://www.domain.com/list and .htaccess sends that request to index.php?section=list.
So, this is my current .htaccess:
RewriteEngine On
RewriteBase /dir
Redirect on HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteRule /([a-zA-Z]+)/$ /index.php?section=$1 [NC,L]
Testing it on htaccess tester seems to work as expected, but not on my webserver.
I've got no more .htaccess files in the server root dir, and by viewing server logs I only get Redirect to non-URL errors.
I've also seen this answer but it didn't help me so far.
Have this .htaccess in /dir/ directory:
RewriteEngine On
RewriteBase /dir/
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ index.php?section=$1 [QSA,L]
This will support pretty URL as http://domain.com/dir/list

Block some internal urls using .htaccess in cakephp

My site is running on cakephp 2.0 version , I want to block some of my site urls.
for example
www.exmaple.com/users/register
www.example.com/users/login
I tried to modify the .htaccess file of root , as well inside app folder but nothing work.
my code is
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
RewriteCond %{REQUEST_URI} ^/register
RewriteCond %{REQUEST_URI} ^/login
</IfModule>
Please help , how can I achieve it.
Thanks
You can use these rules:
<IfModule mod_rewrite.c>
RewriteEngine on
# block these URIs
RewriteRule (register|login) - [F,NC]
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>

htaccess proper redirect with trailing slash

Good day!
I want to make proper redirect by htaccess, when there is GET request like this:
example.org/directory/page/
It should give example.org/page.php
My htaccess lets only redirect this kind of request
example.org/directory/page
I use this config
#remove extension
RewriteRule ^([^\.]+)$ $1.php [NC,L]
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
Thank you for attention!
Reorder your rules and tweak .php adding rule:
#remove directory
RewriteRule ^directory/(.*)$ /$1 [L]
#remove extension
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^([^.]+)/?$ $1.php [L]

How to redirect specific FOLDER to index.php

Hi i have this web sctructure:
/css/
/js/
/img/
/dashboard/
/pages/
/index.php
I got this .htaccess file
# Setting default charset
AddDefaultCharset utf-8
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web
RewriteRule ^([\w\-]+)/?$ index.php?page=$1
</IfModule>
Works ok, if i write
myurl/login redirect to index.php?page=login
but i wanna to redirect this
myurl/dashboard to index.php?module=dashboard
and
myurl/dashboard/login to index.php?module=dashboard&page=login
but isnt work with my .htaccess, can you help me? im trying redirecto ALL to index.php but doesnt work, cause redirect /img, /js, /css folder to index.php and i cant see my web
Thanks in advance.
Have it this way:
RewriteEngine On
RewriteBase /web/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(dashboard)/?$ index.php?module=$1 [L,QSA]
RewriteRule ^(dashboard)/([^/]+)/?$ index.php?module=$1&page=$2 [L,QSA]
RewriteRule ^([\w-]+)/?$ index.php?page=$1 [L,QSA]

HTAccess Redirect all Subpages

I'm trying to redirect all subpages of a page using .htaccess:
RewriteEngine On
RewriteBase /
RewriteRule ^/news-and-events(.*)$ /news/$1 [R=301,L,NC]
The above doesn't seem to be working, what's the easiest way to do this in as few lines as possible?
My HTAccess:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
RewriteRule ^news-and-events(.*)$ /news/$1 [R=301,L,NC]
Remove leading slash:
RewriteRule ^news-and-events(.*)$ /news/$1 [R=301,L,NC]
.htaccess is per directory directive and Apache strips the current directory path from RewriteRule URI pattern.