Why does this modrewrite affect other files? - regex

I have been using this following code to rewrite the request variable in a nested directory.
RewriteEngine on
RewriteRule ^demos/folder1/page\.php$ - [L]
RewriteRule ^demos/folder1/([^/]+)/([^/]+)$ demos/folder1/page.php?slug=$1 [NC]
RewriteRule ^demos/folder1/([^/]+)$ demos/folder1/page.php?slug=$1 [NC] /* ruins everything, with other links *
This is located in the .htaccess file in the root directory, not /folder1/. Any idea why it would be causing other .php files on the server to not display? I need only requests to /demo/folder1/page.php to be rewritten to /demo/folder1/REQUEST. What am I missing?
Thank you in advance!

Try this code:
RewriteEngine on
RewriteRule ^demos/folder1/page\.php$ - [L,NC]
# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^demos/folder1/([^/]+)/?$ demos/folder1/page.php?slug=$1 [QSA,NC,L]

Related

Rewriting URL from other than root folder with .htaccess

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]

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: multiple rewriterule

I want to rewrite the following:
http://www.example.com/map to http://www.example.com/index.php?p=map
and
http://www.example.com/map/1 to http://www.example.com/index.php?p=map&id=1
My current code is:
RewriteEngine On
RewriteRule ^([^/]*)\$ /index.php?p=$1 [NC]
RewriteRule ^([^/]*)/([^/]*)\$ /index.php?p=$1&id=$2 [L]
But if fails...
Who can help me with the right .htaccess code? Thanx in advance!
You need to ignore real files and directories from your rewrite and don't escape $.
You can use:
RewriteEngine On
# rule to ignore files and directories from all rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ /index.php?p=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ /index.php?p=$1&id=$2 [L,QSA]

Trouble with .htaccess and Two Files in Directory

I'm having trouble with my .htaccess file when trying to execute a different file in the same folder based on an extra URL parameter for an automobile website. I'm terrible at regex and really tried to get this working, hope you guys can help.
The directory structure is www.domain.com/car/make/model
My current .htaccess file lives in /car and is the following for the "make":
RewriteCond %{REQUEST_URI} !make\.php
RewriteCond %{REQUEST_URI} /([^/]+)/([^/]+)
RewriteRule !\.(gif|jpg|png|css)$ make.php?make=%2 [L]
This works fine for say www.domain.com/car/honda
When I try to add a new condition and rule above this one for the automobile "model", the rule doesn't fire for the request_uri (it keeps running make.php instead):
RewriteCond %{REQUEST_URI} !model.php
RewriteCond %{REQUEST_URI} /([^/]+)/([^/]+)/([^/])
RewriteRule !\.(gif|jpg|png|css)$ model.php?test=%2&model=%3 [L]
I'm trying to execute a different file (model.php) for, say www.domain.com/car/honda/accord
What am I missing? Hope this makes sense. Thanks much.
Have your rules like this:
RewriteEngine On
RewriteBase /car/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/?$ make.php?make=$1 [L,QSA]
RewriteRule ^([^/]+)/([^/]+)/?$ model.php?test=$1&model=$2 [L,QSA]

Mod_Rewrite Htaccess Internal Redirect Just Not Working

I have tried so many things in the past hour and nothing works.
I have a website called example.com
I have this code:
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+).html$ index.php?p=$1&i=$2 [N]
RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?p=$1
When I go on example/forums/discussion-f3.html it works.
When I try /forums/introductions-f2.html, it redirects me to /index.php/?p=forums&i=introductions-f2. Also, http://craftshaft.org/forums/ sends me to http://craftshaft.org/index.php/?p=forums but when I do it without the slash and remove it from the code, it works, but I need the slash at the end so it looks like a folder.
Basically, I'd like to be able to view URLs like this:
/forums.html (to /index.php?p=forums)
forums/thread-name.html (to
index.php?p=forums&i=thread-name)
Place this code in root .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^ - [L]
RewriteRule ^([\w-]+)(\.html)/?$ index.php?p=$1 [L,NC,QSA]
RewriteRule ^([\w-]+)/([\w-]+)\.html$ index.php?p=$1&i=$2 [L,NC,QSA]