.htaccess mod_rewrite trailing slash - php

I'm having issues with my mod_rewrite module here. If I do /toronto/ it will direct me accordingly, however, if I do /toronto without the trailing slash it comes back with a 404. I need /toronto and /toronto/ to both read from the /city_name folder. How can I avoid having the trailing slash, here's my code:
RewriteEngine On
RewriteBase /city_name
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /([^\./]+)\.php$
RewriteCond %{DOCUMENT_ROOT}/city_name/%1.php -f
RewriteRule ^(.*)/([^\./]+)\.php$ /city_name/$2.php?page=$1 [L,QSA]

RewriteRule (.*)/$ /city_name/index.php?page=$1 [L,QSA]
This rule explicitly says that the URI has to end with a slash, so 'toronto' matches no rules. To make the slash optional, use the ? operator:
RewriteRule (.*)/?$ /city_name/index.php?page=$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.

.htaccess: Rewrite dynamic url with static url

I want to enter http://localhost:81/admin/dashboard in my browser but the request should be http://localhost:81/admin/index.php?page=dashboard.
The mod_rewrite is enabled and i tried this in the .htaccess but it didn't work. The .htaccess is located in htdocs/admin/.htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ index.php?page=$1 [NC]
You can match the trailing slash optionally by adding a ? next to it in the pattern :
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ index.php?page=$1 [NC]

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

add trailing slash to slug url

I have this rewrite condition
I use it to grab the slug url and search for a blog post like this:
http://localhost:8080/blog/my-first-post
So I grab the slug and then look for the post and display it.
Right now it accepts this two forms
http://localhost:8080/blog/my-first-post
http://localhost:8080/blog/my-first-post/
How should I change the .htaccess so it always adds or redirects with the slash at the end.
And how should I change it to remove the slash always?
Here is the .htaccess I have now which I have it in the blog directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
You can replace your current htaccess code by this one
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
# add trailing slash
RewriteCond %{REQUEST_URI} !/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ $1/ [R=301,L]
# remove trailing slash
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+?)/$ $1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
Right now, it adds trailing slash. If you want to remove it, comment add trailing slash block lines and uncomment remove trailing slash block lines

.htaccess rewrite three filenames into two directories

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]

Categories