Q: htaccess rewrite rule - apache

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.

Related

how to dynamically rewrite filepath .htaccess based on domain?

I have this apache rule in my .htaccess file:
RewriteCond %{HTTP_HOST} ^testURL.localhost.local
RewriteRule ^index.php$ /_testURL/index.php [L]
How would I write this so that testURL2.localhost.local or any other subdomain would be rewritten to the corresponding directories?
For instance testURL2.localhost.local would be rewritten to _testURL2/index.php etc
I already tried the option below but I didn't get the intended result:
RewriteCond %{HTTP_HOST} ^testURL
RewriteRule ^cms.php$ /_%1/cms.php [L]
With your shown samples, attempts; please try following htaccess rules file. I have posted 2 sets of htaccess rules file here, you have to use ONLY one set at a time.
1st solution: This is specifically for host testURL.localhost.local and will look for either index.php OR cms.php only in UI.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?(testURL)\.localhost\.local$ [NC]
RewriteRule ^((?:index|cms)\.php)$ _%1/$1 [NC,L]
2nd solution: A Generic solution, where it will look for anyvalue.localhost.local and it will add anyvalue in path while rewriting, also it will look for any php files in uri.
RewriteEngine ON
RewriteCond %{HTTP_HOST} ^(?:www\.)?([^.]*)\.localhost\.local$ [NC]
RewriteRule ^([^.]*\.php)$ _%1/$1 [NC,L]
NOTE1: Also please make sure to clear your browser cache before testing your URLs.
NOTE2: Please keep these rules at the top of your htaccess rules file.
NOTE3: I am also additionally/optimally matching www. in host in case you don't want it you could remove it (?:www\.)? part in condition.

Redirect with RewriteRule apache

I want to make an internal redirect with this result:
My old link is like: http://www.example.com/en/some-other-things
My new link should be: http://www.example.com/some-other-things
Basically I want to 'remove' the /en/ and get all the rest of the url in the new url. I've tried this rules in the .htaccess without success:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule "^/en/(.+)" "/$1" [R=301,L]
</IfModule>
Make leading slash optional so that rule works both in Apache config as well in .htaccess files. Use .* instead of .+ to ensure you match example.com/en/ as well. Also there is no need to quote the pattern or target.
RewriteEngine On
RewriteRule ^/?en/(.*)$ /$1 [R=301,L,NC,NE]

Apache mod-rewrite rule not working

I am facing issue with apache rewrite rule. i am expecting the below result
If URL is
http://www.mysite.co/testWeb/query1/guery2/3
Then Result
http://www.mysite.co/testWeb/index.html/#/query1/guery2/3
(index.html/# - has to be added in URL, it the path contains testWeb)
The rule i added is as follow
RewriteEngine On
RewriteRule ^testWeb/(.*)$ http://%{HTTP_HOST}/testWeb/index.html/#/$1 [L]
this rule is working fine in the rewrite rule online tester. But not in my apache. Any idea?
It looks like the Rule is creating a Redirect loop error on your server because your Regex pattern also matches the target url /testweb/index.html .
You need to use a negitive RewriteCond to prevent this .
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/testWeb/index\.html$ [NC]
RewriteRule ^testWeb/(.*)$ http://%{HTTP_HOST}/testWeb/index.html/#/$1 [NC,L,NE]

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]

How to use RewriteRule in Apache to redirect from /abc/ to /abc?

RewriteRule ^([^/\.]+)/$ $1 [R] redirects from website.com/abc/ to website.com/home/user/www/abc
How do I redirect to the correct location? (website.com/abc)
The first part of the RewriteRule is the matching part, the second the replacement expression. For full-url RewriteRules they should start with a ^/ (slash), because a path after http://exampl.com always starts with the root-slash. To redirect http://example.com/abc/ to http://example.com/abc (remove the trailing slash) you can do this:
RewriteEngine On
RewriteRule ^/([^/]+/$ /$1 [R]
It looks like the URL is already being rewritten to a filename and then you are rewriting the filename. This usually happens when the rewrite rule is being specified in an .htaccess file. The problem is due to the fact that by the time Apache reads the .htaccess file, it has already resolved all URLs to filenames. The standard solution to this is to supply a RewriteBase rule in your .htaccess. Try:
RewriteBase /
Above your RewriteRule for starters.
More documentation is here
You could try it with the additional L flag to avoid any further rules to be applied:
RewriteRule ^([^/\.]+)/$ /$1 [R,L]
But if nothing else works, try this:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ (/[^/.?\ ]+)/[? ]
RewriteRule ^[^/.]+/$ %1 [R]