how to remove forward slash from specific url using htaccess - php

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.

Related

RewriteCond subdirectory is not working

I am trying to do RewriteRule for subdirectory and I could not do it.
This is the code:
RewriteCond %{REQUEST_URI} !^/subfolder
RewriteCond %{DOCUMENT_ROOT}/subfolder%{REQUEST_URI} !-f
I use the following code with the above as well to remove the .php extension and add a trailing slash at the end.
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]
I can access any page except index.php
I need someone to help me fix this issue so I can access the pages/files in the subdirectory.
Thank you
You need to explicitly check if the php file exists before you add the .php extension. And remember that rewrite conditions only apply to the immediately following rule, so you need to duplicate them if they need to apply to multiple rules. Also, you want all your redirects to happen before any routing or rewrites:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^([^/]+)/$ $1.php [L]
RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
# whatever subfolder rewrites you need can go here

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]

Redirect two files with htaccess

this is my scenario. My application urls are showed like this:
example.com/index.php/article/whatever/here
in order to remove the index.php from url I have put this in my .htaccess and works fine:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Now, my problem is that I have include and admin.php like this:
example.com/admin.php/manage/whatever/here
And I don't know how to remove the admin.php from url without make conflics with the index.php because I have tried this and a 500 Internal Server Error is showed:
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ /admin.php/$1 [L]
EDIT
Another try was this and nothing, because a 404 error is showed:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
ALL my urls should have several parameteres as requiered:
example.com/this/is/a/large/url/because/is/requiered
EDIT 2:
I have create two rules, each rule alone works fine!!! but both can't work together:
RewriteRule ^admin/(.*)$ /admin.php/$1 [L]
RewriteRule ^(.*)$ /index.php/$1 [L]
Try:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond ${REQUEST_URI} ^admin/(.*)$ [NC]
RewriteRule ^(.*)$ /admin.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
Then at least test this with:
http://www.example.com/some/url
http://www.example.com/admin/some/url
You can slightly modify your rewrite rule to capture the first part of the path, and use it in the replacement. Try this:
RewriteRule ^/(.*?)/(.*) /$1/$2 [L]
Basically the () create a capturing group, so anything between the first two / will be captured as $1 and anything after that will be captured as $2
Note that you can also change your rewrite conditions, if you only want this replacement to apply to index.php and admin.php

Having problems adding '/' to end of URI

Right now i have a url which is like
http://www.example.com/customer/login
i want the URI to always have a ending trail because ill use redirects with ../
and if it doesnt have slash it messes everything up if it has a slash it works fine. I tried to look at some examples online but i couldnt really get anything to work heres my current .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
Anubhava's got the basic answer and I modded him up. You need to send an HTTP redirect to get the browser to request the URL with the / at the end.
To merge with your existing rewrite rules, you should do:
RewriteEngine on
# First check it's not a file
RewriteCond %{REQUEST_FILENAME} !-f
# And it doesn't end in /
RewriteCond %{REQUEST_URI} !.*/$
# Send the redirect. I would do 301 (permanent) here
# the "L" means the rest of the rules are ignored for this request
RewriteRule ^(.*)$ $1/ [L,R=301]
# Now pass thru to your old ruleset URLs the slash-checker didn't catch
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
How about this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ $1/ [L,R=302]
Try using following rules
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?rt=$1 [QSA]
#following rule checks if url is not ending with / and
#if not then redirected to url which is ending with /
RewriteCond %{REQUEST_URI} !.*/$
RewriteRule ^(.*)$ $1/ [R=302,L]

Categories