Confused about the way this .htaccess rule is supposed to work - php

I have this in my .htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]
in my php I have
header("Location: /users/" . $_SESSION['user']);
it loads this into the browser
foo.bar/users/s2xi
which gives me a 404 Not Found error, I can understand up to the part where the directory users doesn't exist, but isn't my RewriteRule supposed to take care of that?
Edit: My complete .htaccess
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^([a-zA-Z0-9_\s-]+)/([a-zA-Z0-9_\s-]+)(/)?$ users.php?type=$1&user=$2 [NC,R]
#RewriteRule ^users/?$ users.php [L]
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
#RewriteRule ^users/([^/]+)/?$ users.php?user=$1 [NC,L]
#RewriteRule ^(admin)/(.+)/?$ users.php?user=$1 [NC,L]
RewriteRule ^admin/([^/]+)$ users.php?user=$1 [NC,L]
#error codes
RewriteRule ^/error/([^/]+)/ error.php?code=$1 [NC]

Change your .htaccess to the following:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^users/([^/]+)$ users.php?user=$1 [NC,L]
Also make sure users.php is in a right directory. If it's in root directory, you can use:
RewriteRule ^users/([^/]+)$ /users.php?user=$1 [NC,L]

Related

removing index.php from site in php

Whenever I try logging into my site with a wrong user details, I get redirected from www.example.com/ to www.example.com/index.php.
I would like to remove index.php from my link.
Here is the content of my htaccess file, which does not work for me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.php\ HTTP/
RewriteRule ^(.*)\.php$ /$1 [R=301,L]
RewriteRule ^login index.php [NC,L]
Check if this can work for you:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Check mod_rewrite module is enabled if not you first enable it.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
Reference
htaccess documrntation

Too many redirects. Rirect path to subfolder using htaccess

I've ulploaded wordpress in subdirectory to intall. Path of it is
public_html/revslider-standalone
This is full path
http://greenlinerenovations.com/revslider-standalone
When I open this URL in browser, it says too many redirects. Below is my code for .htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^About+$ about.php
RewriteRule ^FAQ+$ faq.php
RewriteRule ^Contact+$ contact.php
RewriteRule ^Gallery+$ gallery.php
RewriteRule ^Areas-we-service+$ cities.php
RewriteRule ^(([^/]+/)*[^.]+)$ servicesparse.php?name=$1
What rule should be added or corrected to install Wordpress on that path ?
Redirects in htaccess file is neccessory so can't remove it. Is there anyway to add new rule and it works ?
Use:
RewriteEngine On
RewriteRule ^About$ about.php [NC,L]
RewriteRule ^FAQ$ faq.php [NC,L]
RewriteRule ^Contact$ contact.php [NC,L]
RewriteRule ^Gallery$ gallery.php [NC,L]
RewriteRule ^Areas-we-service$ cities.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(([^/]+/)*[^.]+)$ servicesparse.php?name=$1
With RewriteCond before the last rule RewriteRule

.htaccess remove index.php from subdir redirects all dynamic pages

Trying to remove index.php from and just resolve to the subdirectory name which works, however after making the change to .htaccess file all the other pages redirect to the subdirectory.
For example:
Changed: site.com/subdir/index.php to site.com/subdir/ but then site.com/subdir/page?id=4 resolves to site.com/subdir/
.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteBase /subdir/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
How to write this rule so that only index.php resolves to /subdir/ and not the other pages within that directory?
You can use these rules in site root .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(.*/)index\.php[?\s] [NC]
RewriteRule ^ %1 [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ subdir/index.php [L]

RewriteCond exclude folder

I have the following in my .htaccess:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
This works nicely, but I don't want the RewriteRule to trigger for the following folder and its subfolders.
www.site.co.uk/Content
How do I do this?
Try this Rule,
RewriteEngine on
RewriteRule ^(content)($|/) - [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
or create a .htaccess in content directory and put
RewriteEngine off

.htaccess RewriteRule error

I have these lines in .htaccess.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php
Now, I'd like to add the following into the .htaccess, so I can redirect all users to http://www.mydomain.com
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
So, I just added the code above in my existing .htaccess file.
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_HOST} ^mydomain.com
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L]
RewriteRule . index.php
But, It doesn't work as I expect.
Please teach me how I am supposed to write in this case.
Thanks so much in advance!!
You should redirect to the www subdomain first, then forward the request to your index.php file. Try this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.).+$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

Categories