Rewriting a number based URL using .htaccess RewriteRule - apache

How can I rewrite a simple number based URL to a sub folder?
I want http://mysite.com/123 to redirect to http://mysite.com/en/123.
The number could be anything from 1 - 9999. My attempt in htaccess was:
RewriteRule ^([0-9]+)$ /en/$1/ [R]
But that doesn't work.

your syntax is right, I just remove slash in the end of line and it works:
RewriteRule ^([0-9]+)$ /en/$1

Make sure that this 3 lines are writen in your HtAccess File (sorry for mybad english)
Options +FollowSymlinks
RewriteEngine on
RewriteBase /
##
## Now the rewrite
RewriteRule ^([0-9]+)$ ./en/$1

Related

Replacing Strings with .htaccess in URL

I need to edit my .htaccess file so that this string:
https://example.com/haathumb.php?var=400x300/src/wp-content/uploads/2017/08/brain-training-game-show-app-improves-memory.jpg
Is replaced with this replaced with this string:
https://example.com/thumb/400x300/src/wp-content/uploads/2017/08/brain-training-game-show-app-improves-memory.jpg
To sum up I need to replace haathumb.php?var= with thumb in a string using .htaccess. How do I do that?
A redirect from haathumb.php?var=... to thumb/... then?
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)var=([^&]+)
RewriteRule ^haathumb\.php$ /thumb/%1? [L,R=permanent]
I ended up using the .htaccess code below to make haathumb work correctly:
# BEGIN haathumb rewrite
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^thumb/(.*) /haathumb.php?var=$1 [L]
</IfModule>
# END haathumb rewrite

Issue with specific 301 redirection in htaccess

I'm having an issue with a format of URL.
I need to send
/en
to /
The problem is that I need only /en (exactly), not /en/foo...
I know how to do it with string that ends with .html, but here I have other URLS that have /en/stuff and they are also matched.
Would really appreciate assistance...
You need to specify end of string in RewriteCond e.g.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/en$
RewriteRule ^(.*)$ / [L,R=301]
Please note that Condition pattern as well as Rewrite Rule pattern are a perl compatible regular expression (with some additions):
For Anchors:
^ - Start-of-line anchor
$ - End-of-line anchor
EDIT
BTW, there is very nice htaccess tester at http://htaccess.madewithlove.be/ which you could use to test the rules.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteRule ^en/?$ / [L,R=301,NC]
This will redirect /en or /en/ to / also note that /EN will also be handled because of NC (Ignore Case) flag used here.

.htaccess rewrite /dir/sub to /dir

I have files in /directory/subdirectory and I'd like it to appear to users that they're in /directory.
Here's what I have in my .htaccess file at the moment (returns 500 error):
RewriteEngine on
RewriteRule ^directory/(.*) directory/subdirectory/$1 [NC,L]
I know that rewritebase could accomplish this if my files were in the root www directory but I can't do that for this project.
Any ideas?
Thanks!
You need to add [NC,L] at the end of your rewriterule line. NC indicates that this rule is case insensitive, and L indicates Last, that's where the engine stops processing the rules.
For more info visit ModRewrite cheatsheet
Use this rule:
RewriteRule (?!^directory(/.*|)$)^(directory)/(.*)$ $1/subdirectory/$2 [NC,L]

replace a string in url in .htaccess

I would like to redirect www.hostname.com/some path/?cpao=12 to www.hostname.com/some path/?kmas=12.
Essentially replacing the word cpao with kmas and keeping everything else the same.
Any help will be appreciated.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^cpao=([^&]*) [NC]
RewriteRule ^ %{REQUEST_URI}?kmas=%1 [R,L]
RewriteRule ^hostname.com/somepath/\?cpao([0-9]+)$ hostname.com/somepath/\?kmas=$1
you use regex in htaccess $1 is the numbers that was catched in group 1

modrewrite RewriteRule problem

I am using Apache 2.2 and mod_rewrite. I would like to take the following urls and map them to another url. The pattern is simple and like this:
http://domain/test/ex1.html -> http://domain/app/index.php/content/?name=ex1
I tried the following in an .htaccess file (place in /test/ directory in docroot):
RewriteEngine On
RewriteBase /app/
RewriteRule (.*)\.html index.php/content/?name=$1
and
RewriteEngine On
RewriteRule (.*)\.html /app/index.php/content/?name=$1
I wasn't sure if the backreference was correct so set to $0 and $2 but never seemed to help.
I also tried setting the RewriteLogLevel to 9.
There is a step where it is almost there:
rewrite 'ex1.html' -> 'index.php/content/?name=ex1'
The last line of the rewrite log is as follows:
[perdir /var/www/domain/htdocs/test/] internal redirect with /app/index.php/content/ [INTERNAL REDIRECT]
How can I get this to rewrite to /app/index.php/content/?name=ex1 ?
EDIT
so using this as an .htacces in the docroot file works for the RedirectMatch:
RewriteEngine On
RewriteBase /
RedirectMatch ^(.*)/(.*)\.html$ http://domain/app/index.php/content/?name=$2
#this doesnt work - adding to docroot allows for it to pick up but still using test/ex1 rather than just ex1
#RewriteRule ^(.*)\.html$ /app/index.php/content/?name=$1
Any help getting the bottom RewriteRule (that is currently commented out) to work would be appreciated.
thanks
In your example you mentioned domain/test/ so I'm going based of rewriting from /test/*.html to /app/index.php/content/?name=
RewriteEngine On
RewriteBase /
RewriteRule ^test/(.*)\.html$ /app/index.php/content/?name=$1
That should work.
Uhm, off the top of my head:
RewriteCond %{REQUEST_URI} ^/app/(.*)\.html$
RewriteRule ^/app/(.*)\.html$ /app/index.php/content/?name=$1
This assumes that the rewrite base is /