.HTACCESS Mod Rewrite with NO suffix? - apache

I'm trying to get the following Mod Rewrite to work with no trailing slash as a suffix or file name.
RewriteEngine On
RewriteRule ^test/([^/]*)/([^/]*)/$ /example1/example2/index.php?brand=
$1&video=$2 [L]
The above works however if I remove the trailing slash from /$ it wont work.
So just to recap:
www.example.co.uk/example/example2/index.php?brand=x&video=y
rewrites to
www.example.co.uk/test/brand/video/
but I can't get it working for
www.example.co.uk/test/brand/video

Try changing the regular expression from ^test/([^/]*)/([^/]*)/$ to ^test/([^/]*)/([^/]*)/?$

Related

Trailing slash mod_rewrite rule for certain subfolders

I am trying to get a trailing slash mod_rewrite rule to work for only certain subfolders.
I have article URLs like ////. For example an article URL may be /news/role-playing-games/new-roleplaying-game-released/. Sometimes, for whatever reason, people try the URLs without the trailing slash and they get a 404. I would like instead for apache to add a trailing slash to only the URLs that match certain subfolders.
I tried this rule, but it does not work.
RewriteCond %{REQUEST_URI} ^/(news|reviews|tutorials|guides)/(.*)$
RewriteRule ^(.*)$ /$1/ [L,R=301]
Can anyone tell me what I am doing wrong?
Thanks in advance.
Try
RewriteRule ^((news|reviews|tutorials|guides)/[^/]+/[^/]+)$ $1/ [L,R=301]
I assumed that the url structure you gave is the only one.

Removing file extension with Rewrite in .htaccess

I am currently trying to do some url rewriting. I have made some progress but I can't seem to get this to remove the file extension.
The url it outputs current looks like:
article-install-apache-on-linux.php
I would like it to look like:
article-install-apache-on-linux/
RewriteEngine on
RewriteRule ^article-(.*).php$ ./article_show_friendly.php?url=$1
Just remove the .php from the first part and add an optional slash /?. Also, change (.*) to (.+) to ensure 1 or more characters.
RewriteRule ^article-(.+)/?$ /article_show_friendly.php?url=$1 [L]
Also added the L flag on the end to ensure rewriting stops at that point.

Replacing slashes with dashes in URL Rewriting

How do I accomplish URL rewriting for the following with mod_rewrite in Apache?
Pretty Link : www.mysite.com/pages/category/page/
Actual File : www.mysite.com/html/category-page.html
I only want to rewrite URLs if they contain the domain and the pages directory. In all other cases I want the server to work normally.
I've come up with this but would like to know how to replace slashes with dashes:
RewriteEngine On
#Look for the word "pages" followed by a slash, and then the article title
RewriteRule ^pages/(.+)$ html/$1.html [L]
See if the following rule set works:
RewriteEngine On
RewriteRule ^pages/(.+?)/?$ html/$1.html [N]
RewriteRule ^html/([^/]+)/(.*)$ html/$1-$2 [N]

Ignore trailing slash with Apache Rewrite

I'm using mod_rewrite to redirect like so:
RewriteRule (work)/?$ $1.php [L]
This sends any URL ending in /work or /work/ to work.php
The problem is, when a trailing slash is included, it treats it as a directory, and not the file that it really is. This, of course, breaks all of my relative paths in the file.
I don't mind having a slash in the URL, but is there any way to use Apache to ignore the trailing slash, and treat the URL as a file, just as it would without the slash?
Since you don't want the URL to look like www.domain.com/work/ here's what you can do:
RewriteEngine On
RewriteRule ^work/$ http://www.domain.com/work%{REQUEST_URI} [R=301,L,NC]
RewriteRule (work)$ $1.php [L,QSA,NC]
This will redirect /work/ to /work and /work/?page=main to /work?page=main

Rewrite Trailing Slash Issue

Here is my .htaccess file
Options +FollowSymlinks
RewriteEngine on
ErrorDocument 404 /404.php
RewriteRule ^(\d*)/(.*) /page.php?id=$1&slug=$2
It all works fine. But the moment I type site.com/342/my-page/ (with the trailing slash) I get a 404.
I need the trailing slash as optional. I.e it will redirect to the correct page with or without the slash.
I tried this, but it didn't work
RewriteRule ^(\d*)/(.*)/?$ /page.php?id=$1&slug=$2
Any ideas?
.* is greedy, so it will eat your trailing slash even if it does not have to. You have to force it to stay away like this:
RewriteRule ^(\d*)/(.*[^/])/?$ /page.php?id=$1&slug=$2
This is ensure that $2 is never ending with a slash