Modify rewriting including punctuation?

I ran into a bug on my site: basically this will not allow page names with full stops (periods) in them. I know the root of the problem is in my .htaccess

file.

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?section=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]

      

Here the regex won't match anything with a period, this is intentional if the user is trying to access the file directly.

Here are some examples of common URLs.

games/Spider:+The+Secret+of+Bryce+Manor
games/Orbital
features/Interviewed:+Dennis+Sijnen+from+No+Monkeys
news/all/4

      

The portion of the split

URL is usually only used for pagination. The part of the page

url where I want to place full stops, for example:

games/Must.Eat.Birds

      

Since I'm not particularly good with mod rewriting or regex, I just wish the solution would allow it to stop completely. I know this could potentially be a more complex regex, and I'm not in my depth here.

Thanks for any help.

0


source to share


2 answers


Simons answer will solve your problem, just replace the "page" part [^/]

instead [^/\.]

, which by the way, you don't need to avoid periods in sets.

A bit of mod_rewrite magic that you might be interested in based on the text of your question: this part of the rewrite will match any real files, links, or directories in your DocumentRoot - if that happens in the first place, it doesn't match your last rule - so the provision /css/main.css

goes to a real file. Then everything else is rewritten and dumped into index.php.



# -s = File Exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
# -l = Is a SymLink
RewriteCond %{REQUEST_FILENAME} -l [OR]
# -d = Is a Directory
RewriteCond %{REQUEST_FILENAME} -d
# if we match any of the above conditions - serve the file.
RewriteRule ^.*$ - [NC,L]

# adding your rules with the slight modifications pointed out above and by simon.
# only allows '.' in the "page" portion.
RewriteRule ^([^/.]+)/?$ index.php?section=$1 [L]
RewriteRule ^([^/.]+)/([^/]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]

      

+2


source


Replace each [^/\.]

with [^/]

- this way they will match everything that is not "/", not everything that is not "/" and not "."



If you don't want to allow periods at certain positions, just don't change the old regex at those places.

+2


source







All Articles