.htaccess rewrite three filenames into two directories - php

I am having some problems with using .htaccess to rewrite three filenames.
Basically the url: domain.com/post.php?category=uncategorized&title=title-of-post&id=1
should be rewritten as
domain.com/uncategorized/title-of-post-1
I worked out how to just rewrite one of the filenames (such as the categorized part) but how do I do all three, so that the category will be in one directory, and the second directory will be a merge of the title and id separated by a dash?
This is my code so far:
Options +FollowSymlinks
RewriteEngine on
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w\d~%.:-]+)/?$ category.php?id=$1 [L,QSA]
I also need the URL to end with a slash. If it is not, it will redirect to it.

You can insert a new rule for post.php:
Options +FollowSymlinks
RewriteEngine on
# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([^/]+)/(.+?)-([^/-]+)/?$ post.php?category=$1&title=$2&id=$3 [L,QSA]
RewriteRule ^([^/]+)/?$ category.php?id=$1 [L,QSA]

Related

how to remove forward slash from specific url using htaccess

I have the following .htaccessfile
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
And I have the following url:
http://localhost/mysite/loginPage/
I want to ensure that this url will never ends with forward slash (loginPage/).
I tried to use the following lines:
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteRule ^([^/]+/[0-9]+/[0-9]+/[0-9]+/[^/]+)/.+$ - [L,R=404]
But I get error message in all cases that page is not redirecting properly. Please have in mind that I want this just for the specific URL above, i don't care for the rest of the site.
It depends on where you put the rule for your rewrite. The examples you provided do work. But you have to put them before your last htaccess line.
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
This will create the proper output, and can be tested through something like a htaccess tester
If you want it specifically for just that url you could resort to:
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^mysite/loginPage/$ mysite/loginPage? [R=301,L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
You could also specify an absolute redirect with
RewriteRule ^mysite/loginPage/$ http://%{HTTP_HOST}/mysite/loginPage? [R=301,L]
I want this just for the specific URL above
In that case, your rule should just match specifically this URI rather than .*/.* etc.
Have your rules like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(/mysite/loginPage)/$ [NC]
RewriteRule ^ %1 [L,R=301]
RewriteCond $1 !^(index\.php|resources|robots\.txt) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php/$0 [L]
You don't really need QSA flag in last rule also.
Make sure to clear your browser cache before testing this change.

Rewrite folders and Remove the trailing slash at the end

I'm trying to have only one URL without the trailing slash at the end of the URLs/folders, but at the same time I want to re-write this folder to use dynamic pages, for example:
if I have:
mydomain.com/folder
mydomain.com/folder/
and
mydomain.com/folder/second
mydomain.com/folder/second/
then I want to open only "mydomain.com/folder" and "mydomain.com/folder/second" and this URLs should open a dynamic pages. This is what I am trying, but it does not work, it crash my page:
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L]
RewriteRule ^(.*)$ includes/category.php?url=$1&pageType=category [L]
RewriteRule ^(.*)/(.*)/$ /$1/$2 [L]
RewriteRule ^(.*)/(.*)$ includes/subcategory.php?url=$1&pageType=subCategory [L]
What am I doing wrong? can you help me to run the right syntax please?
You need to redirect the trailing slash URL's instead of just rewriting them. You also probably need to add some conditions to the rules:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /includes/category.php?url=$1&pageType=category [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)$ /includes/category.php?url=$1&pageType=subCategory [L]
RewriteEngine On
RewriteRule ^([^\/]+)/?$ includes/category.php?url=$1&pageType=category [L]
RewriteRule ^([^\/]+)/([^\/]+)/?$ includes/subcategory.php?url=$1&pageType=subCategory [L]
you may also add the following conditions (before rewrites) to exclude real file and folders from those rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

Multiple RewriteRules & RewriteConds interfering

So I have the following piece of code in my .htaccess:
RewriteEngine On
# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]
# Rewriting data from URL
RewriteRule ^minecraft/(.*)/$ /minecraft/$1/ [NC,L]
RewriteRule ^game/(.*)/$ /game/$1/ [NC,L]
RewriteRule ^tool/(.*)/$ /tool/$1/ [NC,L]
# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.*)$ ./router.php?r=/$1 [QSA,R,L]
I want to achieve the following:
A the user can visit "domain.com/contact/", which will show them the contents of the file "contact.php" in the root directory. (Notice the trailing slash in the URL)
The user can visit "domain.com/random-id/" and that request will be routed to "router.php?r=random-id".
The user can visit "domain.com/minecraft/random-name/", and that request will be routed to the "minecraft/random-name/"-folder. (With an index.php etc inside)
I cannot seem to figure it out, any help is appreciated!
Have it this way:
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301]
# Remove .php from URL and add trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
# Redirection if file or folder doesn't exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ router.php?r=/$1 [QSA,L]

PHP .htaccess exception some of .php file

I have .htaccess that the function is to hide .php extension and convert user.php?user=username to be :
http://example.com/hidayurie
Now I have a problem, I have file search.php. When I run the URL : http://example.com/search?q=username. It still detect that search is username whereas I have file search.php
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com
RewriteRule (.*) http://example.com/$1 [R=301,L]
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^/home/.*$ index.php [QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
How can I set if .php file, then it will open that php file without extension?
Try adding some conditions to your rules to prevent the rewrite if the URI points to a file that exists. You should also make sure you have multiviews turned off (this can be anywhere in your htaccess file):
Options -Multiviews
Then add these conditions before each of these 4 rules:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/$ user.php?user=$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)$ user.php?user=$1&page=$2
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z0-9_-]+)/([0-9]+)/$ user.php?user=$1&page=$2
You need to define search and any other different pages/URLs you wish to rewrite, otherwise yeah it will continue to think it's a user and refer to user.php
Example:
RewriteRule ^search/?$ /search.php
RewriteRule ^search/([a-zA-Z0-9_-]+)/?$ /search.php.php?q=$1
Second line would allow you to query your search with a clean url, ex; yoursite.com/search/username/

How do I modify query strings in the URL to make them more user friendly

I have the following in my .htaccess file, which removes the .php extension, and removes the slashes after the remainder, if it isn't a directory. However, I would like to modify this to take change query strings as well.
AddType text/x-component .htc
RewriteBase /
# remove .php; use THE_REQUEST to prevent infinite loops
RewriteCond %{THE_REQUEST} ^GET\ (.*)\.php\ HTTP
RewriteRule (.*)\.php$ $1 [R=301]
# remove index
RewriteRule (.*)/index$ $1/ [R=301]
# remove slash if not directory
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /$
RewriteRule (.*)/ $1 [R=301]
# add .php to access file, but don't redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) $1\.php [L]
I would like to change {root}/file?id=1 to /file/1
and
I would like to change {root}/directory/file?id=1 to {root}/directory/file/1
Any help is greatly appreciated!
Insert these 2 new rules in the end:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([0-9]+)/?$ /$1?id=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/([0-9]+)/?$ /$1/$2?id=$2 [L,QSA]
PS: Also make sure to use L flag in all of your RewriteRule lines above.

Categories