So I have shortened article.php?id=10 to article/10. And it all seemed to work fine. But little did I know that it ruined the rest of my URLs. So with http://localhost/forgot/, I'd have to go to http://localhost/forgot/index to actually reach it. Here's what I'm using
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1
RewriteRule ^([^\.]+)$ $1.php [NC,L]
I want to go to http://localhost/forgot/ instead of http://localhost/forgot/index/ Any ideas?
Have your rules like this:
Options -MultiViews
DirectoryIndex index.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/([0-9]+)$ article.php?id=$1 [L,QSA,NC]
# rewrite from /dir/file/ to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Related
I have this .htaccess code in the root folder to replace this url: localhost/mysite/profile.php?username="someone" to become localhost/mysite/someone.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /mysite/profile.php?username=$1
This is working fine but what I want is to include subpages like for example the about page to be localhost/mysite/about instead of localhost/mysite/about.php. And the subpages are in the root folder too.
How can I achieve these two rewrites?
You can have this code in mysite/.htaccess:
RewriteEngine On
RewriteBase /mysite/
# add .php internally to files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
# handles profile URLs
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$ profile.php?username=$1 [L,QSA]
Try below code it's working fine to me.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Try This
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/([A-Za-z0-9-]+)$ /mysite/profile.php?username=$1
This is my first htaccess file.
this is what i have written
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
The problem what I am facing is both are pointing to maincategory.
How can I redirect based on number of "/" in the URL?
Thanks in advance.
Rearrange the rules and try it this way. Let me know if that works for you.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule \.(css|js|png|jpg|gif|otf|eot|svg|ttf|woff|jpeg)$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)$ subcategory/index.php?main=$1&sub=$2&cat=$3 [L,QSA,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)$ maincategory/index.php?main=$1&sub=$2 [L,QSA,NC]
I'm trying to replace question marks and equal signs so that I can use URLs like the following:
http://www.mydomain.com/categories/id/23/name/category-name
The above url would be internally redirected to
http://www.mydomain.com/categories?id=23&name=category-name
I am using this .htaccess:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
# external redirect from /view.php?id=1 to /view/id/1
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+([^.]+)\.php\?([^=]+)=([^\s&]+) [NC]
RewriteRule ^ /%1/%2/%3? [L,R=301]
# internal forward from /view/id/1 to /view.php?id=1
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
but it's not working as expected. When I go to http://www.mydomain.com/categories/id/23/name/category-name I get a 500 internal server error. Why? What am I doing wrong?
updated code
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
This rule should take care of recursively replacing each / with = and finally making it a nice Query String:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/([^/]+)$ $1.php?$2 [L,QSA]
You need to comment out or delete this rule:
# RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ /$1.php?$2=$3 [L,QSA]
UPDATE: You .htaccess need re-ordering and little bit of tweaking. This should work:
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /mydomain/
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?(.*)$ $1/$2=$3&$4 [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^/]+)/(.+?)&?$ $1.php?$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
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]
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]