Rewriting URL from other than root folder with .htaccess - regex

I am trying to make URL rewrite to nice looking URL with .htaccess. I was trying so many ways and always get or error 500 or nothing is happening. My folder tree looks like this:
root
index.php
.htaccess
p
.htaccess
citati.php
...
What I am trying to do is from my "p" directory when someone goes to https://example.com/p/citati/Test-test to rewrite that to https://example.com/p/citati?autor=Test-test so I can have $_GET["autor"] in my citati.php file. I have many .php files in "p" directory so that is also what I need to worry about when rewriting.
This is my .htaccess from root
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.+)p/citati\/(.+)$ p/citati?autor=$1 [NC]
And this is what I have now in my "p" directory .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L,QSA]
RewriteRule ^p/citati\/(.+)$ p/citati?autor=$1 [NC]
</IfModule>
It doesn't matter if I can achieve this from root .htaccess file or from "p" directory .htaccess I just want somehow to do this.

As per your shown samples, please do have your root's htaccess Rule file as follows.
RewriteEngine ON
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
For your p directory/folder have that htaccess Rule file as follows. You need to use same rules from root htaccess, you could inherit it.
RewriteOptions InheritBefore
RewriteEngine ON
RewriteBase /p/
RewriteCond %{THE_REQUEST} \s/(citati[^.]*)\.php\s [NC]
RewriteRule ^ %1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/(.*)/?$ $1.php?autor=$2 [NC,L]

Related

htaccess list directory and rewrite rules for it

I'm trying to make a .htaccess code for automatically rewrite rules for a directory.
In example:
URL: https://example.com/assets/test.png
Folder: /src/assets/test.png
or,
URL: https://example.com/js/test.js
Folder: /src/js/test.js
My htaccess file:
#Rewrite to www
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [L,R=301]
#remove html file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
#remove php file extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
How can i make this?
Could you please do try following rule in your .htaccess file once. Please make sure you clear your browser cache before testing your URLs.
RewriteRule ([\w-]+)\.png/?$ src/assets/$1.js [R=301,NC,L]

.htaccess rewrite troubles with /index.php

I have a small site with about 10 pages.
So I have made it so that the URL structure is rewritten via .htaccess simply: www.domain.com/name.php becomes www.domain.com/name
The trouble now is that I added some more pages and folder and want it to recognise: www.domain.com/foldername/
However it just cannot manage it. It gives me a 404 page not found as it is searching for www.domain.com/foldername/.php
My .htaccess code is below. Any help would be greatly appreciated!
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.domain\.com\
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Have your rule check for existence of corresponding .php file before adding .php extension:
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com$
RewriteRule (.*) http://www.domain.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]

How to redirect root to subdirectory using .htaccess

I am currently using shared hosting and have no way to put my files outside the "public_html" folder.
To work around this, I want to rewrite my "root" site to a subdirectory using the script below in an .htaccess file:
RewriteCond %{HTTP_HOST} ^(www.)?example.com$
RewriteCond %{REQUEST_URI} !^/subdir/
RewriteRule ^(.*)$ /subdir/$1
Which works great, except when I navigate to www.example.com/subdir/. That url does not redirect to anywhere, it simply displays the subdirectory. This is a problem for relative urls and such.
Is there a way to continue rewriting my root to a subdirectory while also rewriting www.example.com/subdir/ to www.example.com?
You can use this rule in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{REQUEST_URI} !^/subdir/ [NC]
RewriteRule ^(.*)$ subdir/$1 [L]
And use this rule in /subdir/.htaccess:
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteCond %{THE_REQUEST} /subdir/(\S*)\s
RewriteRule ^ /%1? [R=302,L,NE]

rewrite rule in htaccess

I have a website structured:
- root
- .htaccess
- wordpress installation
- folder xxx
- wordpress
In my .htaccess I have this rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
The problem is that it don't allow me to go in:
www.mywebsite.it/xxx
because a loop redirection happen.
If I erase the rule, all goes good.
How can I modify it?
I want that if user type mywebsite.it/ or mywebsite.it/xxx, it is redirected to www version.
my .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Thanks a lot.
This rule works in isolation but Wordpress stuff is hijacking over it. Slightly modified rule:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Make sure this is 1st rule after RewriteEngine On line in DOCUMENT_ROOT/.htaccess
make sure in WP permalink structure you have site address with www. otherwise WP will again send it back to non-www
If you have any .htaccess below root then they should this additional line below RewriteEngine On line:
RewriteOptions Inherit
Also test in a different browser to avoid 301 caching.

Htaccess - replace .html with a forward slash

I want to redirect URLs of this form:
/page.html?variable=value&othervar=true&thirdvar=100
To this:
/page/?variable=value&othervar=true&thirdvar=100
So basically I just want to replace the .html in the middle of the URL with a forward slash, but I need to preserve the get string that comes with it. This is what I tried:
RewriteRule ^page.html(.+)$ /page/$1 [L,R=301]
But this doesn't appear to be working for me. I've made similar things work recently but I can't figure out what I'm missing here. Thanks for any input.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# external redirect from /example.html to /example
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.html [NC]
RewriteRule ^ /%1/ [R=301,L]
# internal forward from /example/ to //example.html
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{DOCUMENT_ROOT}/$1.html -f
RewriteRule ^(.+?)/?$ /$1.html [L]