Php multiple mod rewrite rule conflict - php

I'm trying clean my url from
localhost/xxx/profile.php?name=google to
localhost/xxx/google
Below is my htaccess file
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([a-z]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [L]
If i remove RewriteRule ^(.*)$ $1.php [L] it's working properly, if i do so then url not opening without .php extension
Is there any way to use both without conflicting each other.

Ordering of rule is causing problem. Your first rule is grabbing every URI. Reverse the order and make your first rule little more smart.
RewriteEngine On
RewriteBase /creatorsink/
RewriteRule ^([^/]+)/?$ profile.php?name=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/creatorsink/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Related

.htaccess conflict between two rewrite rule

in my website two rewrite rule are conflicting with each other
i have url for my users like= http://twekr.com/sam
and all my website pages open like http://twekr.com/login
now here is htaccess file rules
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1
Now only this rule works because its loaded first =
RewriteCond %{REQUEST_FILENAME} !-d
And if i put profile rewrite rule above
RewriteRule ^([a-zA-Z0-9_-]+)$ profile.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ profile.php?user=$1 (then this works)
and this doesnt
RewriteCond %{REQUEST_FILENAME} !-d (does not work)
What is the problem in htaccess rule?
You should check for presence of .php file before adding .php extension:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ profile.php?user=$1 [L,QSA]

Remove .php from URL - Specific case

This is my .htaccess file, everything works so far but I can't manage to remove .php extension from files, every code that I tried from other answers just threw 500 or 404 error. Please advise where and what to add. Structure of the folders is localhost/myfolder/somefile.php
Just to be clear - localhost/myfolder/ is a root for my project.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News
RewriteRule ^product-news/([0-9]+[/])?$ posts.php? p=$1&cat=Product\ News
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1
This snippet will allow you to rewrite to remove php extensions:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
If you want your URL to have a trailing /, you can use this snippet:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
Source
Removing extension, say; php or html in browser will let browser find it a little bit more effort to find the source file. if you need it so, this follows may help you:
UPDATED:
PHP:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)/$ /$1.php
HTML:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)/$ /$1.html
those will remove all extensions in your files (both php and html).
Note: see if server enables mod rewrite module/extension.
You should be using two .htaccess files. The first should go in your localhost root (to redirect requests to myfolder), and the second should go into myfolder (to match up routes against your PHP files):
Root .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ /myfolder/$1/ [L,R=301]
myfolder .htaccess:
RewriteEngine On
RewriteBase /myfolder/
RewriteRule ^team-news/([0-9]+[/])?$ posts.php?p=$1&cat=Team\ News [L]
RewriteRule ^product-news/([0-9]+[/])?$ posts.php?p=$1&cat=Product\ News [L]
RewriteRule ^member-specials/([0-9]+[/])?$ posts.php?p=$1&cat=Member\ Specials [L]
RewriteRule ^ambassador-blogs/([0-9]+[/])?$ posts.php?p=$1&cat=Ambassador\ Blogs [L]
RewriteRule ^user/([0-9]+[/])?$ profile.php?id=$1 [L]
RewriteRule ^browse-all/([0-9]+[/])?$ searchall.php?p=$1 [L]
RewriteRule ^edit/([0-9]+[/])?$ edit.php?id=$1 [L]
RewriteRule ^articles/([0-9]+[/])?$ post.php?id=$1 [L]
Note how I have also included [L] to stop it from doing anything else once it has found a match.
Just below your 301 rule add this rule:
RewriteEngine On
RewriteBase /myfolder/
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,L,NE]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI}\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]

.htaccess multiple rules and exceptions

I have a .htaccess file that has multiple rules to make the url's look "pretty".
This is the file:
RewriteEngine On
RewriteRule ^property/([^/]*)/?([^/]*)/?$ /property.php?ID=$1&Image=$2 [L]
RewriteRule ^property$ /property.php [L]
RewriteRule ^enquire/([^/]*)/?([^/]*)/?$ /enquire.php?ID=$1&Data=$2 [L]
RewriteRule ^enquire$ /enquire.php [L]
RewriteRule ^contact/?$ /contact.php [L]
RewriteRule ^home/?$ /index.php [L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_URI} ^/(property|property/.*|enquire|enquire/.*|contact|contact/.*|home|home/.*)$
RewriteRule ^([^/]*)$ /custompages.php?ID=$1 [L]
The first set of rules works for property, enquire, contact and home.
If the url is not one of these, e.g. www.foo.com/about-us, I want it to call the file custompages?ID=about-us but with this code, it isn't working. I am quite new to using .htaccess files and I can't figure out what the issue is myself.
Your last rule is not correct. Change that to:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ /custompages.php?ID=$1 [L]

Adding a third Mod Rewrite - mod_rewrite

guys. I'm trying to add a third rewrite condition in my .htaccess. The problem is, I have no idea how to add another one. So if anyone has a good in-depth tutorial on it I would be greatful.
Anyway back to business. Here is my current code:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]
The above as you might see removes the .php extensions and makes all files that don't exist go to users(IE: website.com/user goes to user's profile page).
So now I need to add one for the forum. I would like it rewrite like "website.com/Group/Category/Thread".
Is this even possible, or do I need to lower my expectations for this? Any help would be greatly appreciated. Thanks :D
Hmm I remember posting this answer :)
Anyway you can create a new rule on top of previous rules like this (and replace /Group/Category/Thread with whatever you have) :
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^forum/?$ /Group/Category/Thread [L,NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]
You can insert more rules. Given that you want these rules to be matched before your final catch-all to user.php, you should place them before that final rule.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
# match forum, forum/group, forum/group/category and forum/group/category/thread urls
RewriteRule ^forum/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteRule ^forum/([^/]+)/?$ forum.php?group=$1 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2 [L]
RewriteRule ^forum/([^/]+)/([^/]+)/([^/]+)/?$ forum.php?group=$1&category=$2&thread=$3 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ users.php?name=$1 [QSA,L]

URL not rewriting correctly for subdirectories

I am using this code for url rewriting
What its not doing is if there is directory i want to go like this its going to profile.php any idea how to do hide extension php in subdirectory
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?username=$1
try this
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.php [L]

Categories