Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 984
cmal Avatar asked Oct 01 '11 18:10

cmal


People also ask

How do I get rid of trailing slash?

Use the String. replace() method to remove a trailing slash from a string, e.g. str. replace(/\/+$/, '') . The replace method will remove the trailing slash from the string by replacing it with an empty string.

What is $1 in Apache RewriteRule?

$1 represents the match from the first set of parentheses in the RewriteRule regex, not in the RewriteCond regex.

What is RewriteRule in Apache?

RewriteRule specifies the directive. pattern is a regular expression that matches the desired string from the URL, which is what the viewer types in the browser. substitution is the path to the actual URL, i.e. the path of the file Apache servers. flags are optional parameters that can modify how the rule works.

How do I remove the last slash from a URL in Java?

replaceAll("/","");


1 Answers

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

like image 187
Book Of Zeus Avatar answered Sep 21 '22 14:09

Book Of Zeus