What is the modrewrite rule to flatten access to awstats - apache

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]

Related

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.

Mod Rewrite, Unexpected Results

We are trying to redirect everything from one domain to another with the following
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule .? http://www.example2.com%{REQUEST_URI} [R=301,L]
When we visit http://www.example.com/v2sc
We are being redirected to http://www.example2.comv2sc
We would like to be redirected to http://www.example2.com/v2sc considering www.example2.comv2sc is not a valid hostname
Any ideas on how we can accomplish this?
Thank you!
It seems like you're using a .htaccess file for this. In that context the leading slash is not present in %{REQUEST_URI} so it's up to you to put it back in.
RewriteEngine On
RewriteCond %{HTTP_HOST} !=www.example.com
RewriteRule ^ http://www.example2.com/%{REQUEST_URI} [R=301]
Please also note that solutions like this should be used only if you cannot edit the main server configuration file. Doing so would allow you to use a cleaner combination of vhosts and Redirect directives that would run much more quickly.

ReWrite Rules Issue

I seem to be having an issue with my Apache Rewrites
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/?$ / [NC,L,R=301]
RewriteRule ^/$ wordpress/ [NC,L]
I simply need to remove /wordpress from the URL as I have pages within Wordpress I want to be seen as the main directory
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
Any help?
RewriteEngine on
RewriteBase /
RewriteRule ^wordpress/(.*)$ blog/$1 [L]
At the moment the urls are
domain.com/wordpress/blog
I'd rather not have /wordpress, rather domain.com/blog
So it looks like you want to redirect the browser if someone makes a request for domain.com/wordpress/ to a URL without the wordpress bit, then internally rewrite the wordpress bit back into the URI? That's definitely do-able but if you have wordpress rewrite rules somewhere they're not going to play nicely with each other at all.
Any rules in the /wordpress directory will supercede any rules you put in the document root, which is where these rules need to go, and your remove-the-wordress-from-URI rules will be completely ignored. Even if you have rule inheritance turned on, the rules in the /wordpress directory will get executed first.
If all of your wordpress rules are actually in the document root's htaccess file, then just make sure to put these before the wordpress ones:
RewriteEngine on
RewriteBase /
# redirect the browser if someone makes a request for domain.com/wordpress/
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /wordpress/
RewriteRule ^/?wordpress/(.*)$ /$1 [L,R=301]
# internally rewrite the wordpress bit back into the URI
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -f [OR]
RewriteRule %{DOCUMENT_ROOT}/wordpress%{REQUEST_URI} -d
RewriteRule ^(.*)$ /wordpress/$1 [L]

URL Rewriting using .htaccess

To change the URL /mobiles.php?id=5 to /mobiles/5
The content of .htaccess file is as follows:
Options +FollowSymlinks
RewriteEngine on
RewriteRule /mobiles/$1 ^/mobiles.php?id=([0-9]+)$
But still it is showing /mobiles.php?id=5 in the address bar. Please help. Is there anything else needs to be added in the .htaccess file?
Note:
mod_rewrite module is enabled
I have restarted Apache server after making changes to the .htaccess
file
.htaccess file is in htdocs folder of Apache.
I am using Windows + PHP + Apache + MySQL
This works for me:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
RewriteRule ^mobiles.php$ /mobiles/%1? [R,L]
If you see this line:
RewriteRule ^mobiles/([0-9]+)$ mobiles.php?id=$1&rew [L]
I have added rew variable in the query string to prevent Apache to fall in an infinite loop
When Apache execute this line:
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$
Is to make sure that url has not been rewritten for Apache
If your only concern is that the old url stays in the address bar, and you want this not to happen, try adding an [R] at the end.
RewriteRule ^/mobiles.php?id=([0-9]+)$ /mobiles/$1 [R]
Did you actually see the correct page?
By the way, the rewrite rules generally go the other way. I would be expecting to see something like:
RewriteRule ^/mobiles/([0-9]+)$ /mobiles.php?id=$1
Is your concern one of making sure a URL with query parameters does not show up in the address bar?
If I understand correctly, you want
Internally redirect /mobiles/5 to /mobiles.php?id=5
Also redirect the browser TO /mobiles/5 if a user navigates to /mobiles.php?id=5
For this you need 2 rules one to internally rewrite the URL for 1st case and 2nd for browser redirection.
You can do it like this:
RewriteEngine on
# for internal rewrite
RewriteRule ^/?mobiles/([0-9]+)/?$ /mobiles.php?id=$1 [L]
# for browser redirect
RewriteRule ^/?mobiles\.php\?id=([0-9]+)$ /mobiles/$1/ [R,L]
You are doing the opposite, should be:
RewriteRule ^/something/mobiles/([0-9]+)$ /something/mobiles.php?id=$1

Apache Rewrite problems with my blog

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]