Delete slash at end only in .php files - regex

Does it have an access code that killed the slash at the end of the URL?
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [NE,R=302,L]
How to do something like that:
lala.php/
to
lala.php

To remove trailing slash after .php you can use this simple rule:
RewriteRule ^(.+\.php)/$ /$1 [L,NC,R=301]

Related

.htaccess - Add Trailing Slash And Internal Redirect

In my .htaccess file, I'm using the code:
RewriteEngine on
RewriteRule ^learn/(.*?)/(.*?)/ /learn.php?lang=$1&topic=$2
RewriteRule ^videos/(.*?)/(.*?)/ /video.php?lang=$1&topic=$2
which works fine. But it works on http://domain.com/learn/v1 and http://domain.com/learn/v1/ (notice the slash change).
I want to redirect the non-slash version to slash version maintaining the internal redirect above. I tried to add anothe RewriteRule to do that but then it gives me 404.
Any help would be appreciated.
Try:
RewriteEngine on
## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]
# internal rewrites
RewriteRule ^learn/(.*)/(.*)/$ learn.php?lang=$1&topic=$2 [L,QSA]
RewriteRule ^videos/(.*)/(.*)/$ video.php?lang=$1&topic=$2 [L,QSA]

Remove trailing slash and create query string with .htaccess

In my .htaccess file, I would like to remove the trailing slash from the URL without modifying the current setup for the query string.
I tried this in a two-step fashion, but it's not working as I expect:
# Remove trailing slash
RewriteRule ^(.+)/$ $1 [R=301]
# Create query string from canonical URL
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]
Cheers
You can use:
RewriteEngine On
RewriteBase /site/dev/
## Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=302,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

.php ending up in rewritten url after adding trailing slash

I've got an issue when I'm trying to add a trailing slash to non existent files. Here is my rewrite rules
# remove www from url
RewriteCond %{HTTP_HOST} ^www.goautohub.com [NC]
RewriteRule ^(.*)$ http://goautohub.com/$1 [L,R=301]
#rewrite news/article name
RewriteRule ^news/([^/]*)/$ news.php?viewnews=$1 [NC,L]
#remove index from url
RewriteRule ^index\.php/?$ / [L,R=301,NC]
#remove php from url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
The only thing left right now I want to do is rewrite this url
/news/mustang-cobra-model-highlights
to
/news/mustang-cobra-model-highlights/
If I use use something like
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ /$1/ [L,R=301]
which I found from Force trailing slash at end of rewritten query string it works but it screws up all my other ones it there is already a trailing slash. What it does it adds
/.php/ to the end.
I figure I need a way to limit that to just the news page but I can't seem to get the rule right.
The followin rewrite rule should work:
RewriteRule ^/news/(.*)/$ /news/$1 [NC,L]

With htaccess remove .php extension from any file name, add trailing slash with query string

I want to remove .php extension from given any file name with htaccess and add trailing slash with query string. It should work in localhost also.
Case 1:
http://localhost/demo/order/?oid=123&stat=open (in the browser)
to
http://localhost/demo/order.php?oid=123&stat=open (internal)
Case 2:
http://mydomain.com/order/?oid=123&stat=open (in the browser)
to
http://mydomain.com/order.php?oid=123&stat=open (internal)
It should work for any file name like order.php, contact.php, member.php, ...
I tried this, it is working but I want to add trailing slash at the end of the file name with query string
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{QUERY_STRING} ^(.*)$
RewriteRule ^(.*)$ $1.php?%1 [NC,L,QSA]
I tried many, but not exactly reached to my requirement.
Like remove .php extension, add trailing slash, add query string. But not all these in one .htaccess file. I want all these in one .htaccess file as specified in the above requirement.
Plz help me. Thanks in advance.
You can have this code in root .htaccess:
RewriteEngine On
RewriteBase /custom/
RewriteCond %{THE_REQUEST} /custom/(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ %1/ [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ $1.php [L]
Try this:
RewriteRule ^(.*)\.php$ /$1/ [R=301,L,QSA]

ExpressionEngine mod_rewrite Rule to Redirect URLs with Underscores to Dashes

I'm using ExpressionEngine as my CMS and would like to remove underscores from my site's URLs and replace them with dashes.
For example, I've got a URL that is formatted like this:
http://example.com/index.php/menu/friday-lunch
To remove index.php from the URL, I'm using the following mod_rewrite rule:
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Which works, since I can just type in: http://example.com/menu/friday-lunch
On the old site I used underscores instead of hyphens for page URIs, so I wrote a mod_rewrite rule to to redirect URIs with underscores to use dashes.
So friday_lunch becomes friday-lunch using the following RewriteRule:
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
This rule works rather well, except that it 301 Redirects to example.com/index.php/menu/friday-lunch instead of example.com/menu/friday-lunch — notice the addition of index.php.
Here's the entire .htaccess I'm currently using:
<IfModule mod_rewrite.c>
RewriteEngine On
# Removes index.php
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteRule ^([^_]*)_([^_]*_.*) $1-$2 [N]
RewriteRule ^([^_]*)_([^_]*)$ /$1-$2 [L,R=301]
</IfModule>
How can I redirect all of my URLs with underscores to the equivalent with dashes?
Bonus: to make matters worse, URLs that lead to /system, must not be rewritten with a hyphen, e.g.: example.com/system/login_in/.
Here's a complete set of RewriteRules that should do what you need:
<IfModule mod_rewrite.c>
# Enable Apache's RewriteEngine
RewriteEngine On
# Ignore Matching Directories
RewriteRule ^(images|themes|system) - [L,NC]
# Replace Underscores with Dashes
RewriteRule ^([^_]*)_([^_]*)_(.*)$ /$1-$2-$3 [R=301,L]
RewriteRule ^([^_]*)_(.*)$ /$1-$2 [R=301,L]
# Remove index.php from ExpressionEngine URLs
RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
</IfModule>
To have your mod_rewrite rules ignore the ExpressionEngine system folder and not replace underscores _ with dashes - use the following:
RewriteRule ^(images|themes|system) - [L,NC]
Dissecting the RewriteRule into plain English:
The - flag instructions Apache to do nothing, and to not rewrite the URI
The L flags means this should be last rule; ignore everything following
The NC flag means no-case (so "System" or "SYSTEM" is also matched)
This "ignore" rule is especially important and you may need to add additional directories to exclude depending on your directory structure.
Otherwise, you may end up with images and other files saved with underscores that get replaced with dashes.
Note: If your URLs contain more than three underscores, you'll need to add another RewriteRule above the existing ones for each Word Separator for URL Titles you want to replace:
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)_(.*)$ /$1-$2-$3-$4-$5 [R=301,L]
RewriteRule ^([^_]*)_([^_]*)_(.*)_(.*)$ /$1-$2-$3-$4 [R=301,L]
You included 'index.php' in your replacement string.
RewriteRule ^(.*)$ index.php/$1 -> RewriteRule ^(.*)$ $1