Apache Rewrite problems with my blog - apache

I am having a hard time getting the rewrite rule setup correctly for my website&blog. Here is the current line in Apache's virtual host:
RewriteRule ^/(?:blog|apc|_em|phpsecinfo|blog/)/ - [L]
I am able to access my URL at www.domainname.com/blog/
But I am unable to access it at www.domainname.com/blog (without the ending /)
How can I edit my Rewrite rule so that I can reach the blog without the ending / ? Thanks

This should help:
RewriteRule ^/(blog|apc|_em|phpsecinfo)$ /$1/ [R,L]
RewriteRule ^/(?:blog|apc|_em|phpsecinfo)/ - [L]

Add the ? modifier to whatever slash you want to make optional:
RewriteRule ^/(?:blog|apc|_em|phpsecinfo)/? - [L]

Related

mod_rewrite forwarding always forwarding to index page

I have some basic URL forwarding set up in my .htaccess file to create seo friendly links. My problem is that after I added the first two rules any url that begins xxxxxx.com/cm/en forwards to the index page. xxxxxx.com/cm/en/about-condominium-calle-margarita-santa-ana gets forwarded to cm/en_index. I'm sure there is something basic I'm missing here, any help appreciated.
RewriteRule ^cm/en cm/en_index.php [L]
RewriteRule ^cm/es cm/index.php [L]
RewriteRule ^cm/en/about-condominium-calle-margarita-santa-ana cm/en_about.php [L]
RewriteRule ^cm/es/nuestro-apartamentos-calle-margarita-santa-ana cm/es_about.php [L]
The answer for me was to add a $ to the end of ^cm/en.
So I used:
RewriteRule ^cm/en$ cm/en_index.php [L]
RewriteRule ^cm/es$ cm/index.php [L]
I'm guessing the $ means that for that rule to match there can be no more text after the cm/en or cm/es. I'm sure someone else can give a proper explanation!

Rewrite rule preserving post data

I'm trying to build a rewrite rule. I cannot redirect because I need to preserve POST and GET data. In particular, if not present I need to add the string "v1". So:
http://www.example.com/ -> http://www.example.com/v1
I tried:
RewriteEngine On
RewriteRule "/(.*)$" "/v1/$1" [NC,L]
But this is not working. Can you help me please?
EDIT: with the first answer:
RewriteRule !^/?v1/ /v1%{REQUEST_URI} [NC,L]
http://www.example.com -> OK
http://www.example.com/v1 -> not preserving POST data (GET OK)
http://www.example.com/v1/ -> OK, please why (I just added a slash after v1, but this is not the solution I'm looking for)?
Have it this way:
RewriteEngine On
RewriteRule !^/?v1/ /v1%{REQUEST_URI} [NC,L]
EDIT: Since /v1/ is a directory and you're entering http://www.example.com/v1 Apache's mod_dir module adds a trailing / to make it http://www.example.com/v1/ using a 301 redirect. POST data gets lost due to 301 redirect.
To prevent this behavior use this snippet:
DirectorySlash Off
RewriteEngine On
# add a trailing slash to directories silently
RewriteCond %{DOCUMENT_ROOT}/$1 -d
RewriteRule ^(.*?[^/])$ %{REQUEST_URI}/ [L]
RewriteRule !^/?v1(/.*)?$ /v1%{REQUEST_URI} [NC,L]

Q: htaccess rewrite rule

Currently i have this in my .htaccess
# Enable Rewriting
RewriteEngine on
# Rewrite user URLs
RewriteRule ^index/^([0-9a-zA-Z-]+)$ index.php?index=$1
RewriteRule ^([0-9a-zA-Z-]+)$ index.php?index=$1
In my browser, when i access like this :
http://domain.com/aboutus
It is working as expected. What i'm trying to do is, how if i have something like this :
http://domain.com/category/sports
What should i put in my .htaccess so that it can read the URL format for the main-category and subcategory ?
You left out the forward slash in your regex.
RewriteRule ^index/([0-9a-zA-Z-\/]+)$ index.php?index=$1 [L]
RewriteRule ^([0-9a-zA-Z-\/]+)$ index.php?index=$1 [L]
Even better, combine the rules into one:
RewriteRule ^(index/)?([0-9a-zA-Z-\/]+)$ index.php?index=$2 [L]
Also added the [L] flag to stop rewriting when a match is found.

htaccess / mod_rewrite for Search Engine Friendly URLs

I have a bunch of files in the web root: -
/abc-674.php
/def-643.php
etc.etc.
I want to show these when the following URL's are requested, without changing the URL in the browser: -
/products/abc-674/this_can_be_anything.php
/products/abc-674/or_this.php
both redirect to /abc-674.php, and
/products/def-643/this_can_be_anything.php
/products/def-643/or_this.php
both redirect to /def-643.php.
So, basically, the bit between products/ and the next / is the target, while anything after that can effectively be ignored.
If it matters, I've already got a little code in my .htaccess to direct all traffic to my preffered domain (with www): -
# Direct all traffic to domain.com
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.domain\.com [NC]
RewriteCond %{HTTP_HOST} !^domain$ [NC]
RewriteRule ^(.*) http://www.domain.com/$1 [R=301,L]
EDIT: To clarify - the 2nd last line in the above is because I access the site on my dev server via http://domain/ only, and so don't want that rewrite to apply for those requests. I do, however, want this new rewrite to apply to all requests.
Any help greatly appreciated!
I think, you can use the next rule in .htaccess:
RewriteRule ^products/([\d\w]+)/(.*)$ /$1.php

What is the modrewrite rule to flatten access to awstats

My current url to access my awstats is
http://my.server.com/awstats/awstats.pl?config=foo
I would like to use apache and mod_rewrite to
http://my.server.com/foo
My attempt is
RewriteRule ^(.*) /awstats/awstats.pl?config=$1 [NC]
but I just get a 404.
The error.log doesn't give me much help.
Try adding a $ after so that the entire string is eaten by the regexp, and then use [L] so the rewrite engine knows to stop processing and apply the rule.
Remember to switch on the rewrite engine and set the rewrite base.
RewriteEngine on
RewriteBase /
RewriteRule ^(.*)$ /awstats/awstats.pl?config=$1 [NC,L]