the .htaccess is not working - apache

I'm trying to write a simple .htaccess rule to change
https://webxxx.example.net/~test/id/123
to
https://webxxx.example.net/~test/show.php?id=123
But
https://webxxx.example.net/~test/id/123
is now redirected to my 404 page not found.
my .htaccess:
RewriteEngine on
RewriteRule ^\/?~test\/id\/(\w+)$ /~test/show.php?hash=$1
Why doesn't this work?

The solution:
don't need match the reference of public_html folder in regular expression, only in replace
RewriteEngine onRewriteRule ^id\/(\w+)$ /~test/show.php?hash=$1
thanks to all :)

where is the .htaccess placed ? is it under the main root ? or is it under the subfolder (subdomain) ... try to change the place of the .htaccess and make sure it is under the specific subdomain area .

I think this is what you want:
RewriteEngine on
RewriteRule ^id\/([0-9]*)$ /~test/show.php?id=$1
This will just accept numbers after the id.

Related

.htaccess 301 permanent redirect rule

I have this url structure right now.
http://example.com/weather/in-city_name
which I want to permanently redirect to
http://example.com/city_name/weather
Here is what I'm writing to the .htaccess file using this reference
RedirectMatch ^/weather/(.*)$ http://example.com/$1/weather
But this doesn't work.
In my condition city_name is dynamic and manual entry is not possible.
Any suggestion about how to achieve the desire result would be great.
You can use this .htaccess:
RewriteEngine on
RewriteRule ^weather/in-(.+)/?$ /$1/weather [R=301,NC,L]

Apache - rewrite images to php file with .htaccess

I'm looking for a way to rewrite all my image requests from one folder into some.php file, while preserving the original image url (or partial path).
So,
example.com/folder/img/test.jpg
would be rewrited as something like
example.com/folder/some.php?img=img/test.jpg
(is this the best approach?)
I'm not familiarized enought witrh regular expressions, so I'll be very thankfull :)
note : I've tried some solutions before, none of them worked. ALso, I'm running Apache 2.0 under CentOS environment.
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 ^(folder)/(img/[^.]+\.jpg)$ $1/some.php?img=$2 [L,QSA,NC]
Make sure:
.htaccess is enabled
mod_rewrite is enabled
Your URL is http://example.com/folder/img/test.jpg
It sounds like you you want the filename of the image in the url to be included in the new php url, not the entire url. So something like:
RewriteRule ^folder/img/(.*[.]jpg)$ /folder/some.php?filename=$1
Considering what you mention in the comments and that the previous rules didn't work, I edited the message, this is what i have now.
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(.*)\.jpg [NC]
RewriteRule ^folder/img/([\w]*\.jpg)$ folder/some.php?img=img/$1[R=301,L]
If folder is al variable, you can change that for (\w*) and add the reference in the right side of the rule.
Hope this helps.
Bye

Directory rewrite URL in Apache, htaccess

I urgently need help in rewriting php URLs for my site. I have researched and tried everything but it's still not working.
I want to rewrite all the URLs in my htaccess file to rename the directory '_public' to 'art':
www.site.com/_public/work.php
so all url's show:
www.site.com/art/work.php
Can anyone help? would appreciate it very much.
If I understand correctly, you want the user to go to 'www.site.com/art/work.php' and get the page located at 'www.site.com/_public/work.php' displayed.
In .htaccess (in the root directory) do:
RewriteEngine on
RewriteRule ^art/(.+)$ /_public/$1 [L,QSA]
Please add below lines in your htaccess file:
RewriteEngine on
RewriteRule ^art/(.+)$ /_public/$1

Redirect site with .htaccess but exclude one folder

I want to 301 redirect an entire website, but exclude everything in a folder called /uploads which exists in the /root directory.
I have googled for this, but didn't come up with anything, or I didn't think what I saw was right.
Can we crack this?
Try this mod_rewrite rule:
RewriteEngine on
RewriteRule !^uploads($|/) http://example.com%{REQUEST_URI} [L,R=301]
This rule does match any URL path that does not begin with either /uploads or /uploads/ (leading / is missing in the pattern due to the path prefix removal when used in .htaccess files) and redirects the request to the corresponding path at example.com.
Simple answer I just stumbled upon myself.
At the top before any other calls add the following
RewriteRule ^(uploads) - [L]
I think you want this:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/uploads/
RewriteRule (.*) http://www.newdomain.com/$1 [R=301,L]
If you get 500 Internal Error then double-check that you have a space between } and ! on the second line.
A mod-alias based solution
Redirect all except a specific folder
Add the following line to your root/.htaccess :
RedirectMatch 301 ^/((?!uploads).*)$ http://newdomain.com/$1
This will redirect all pages (excluding /uploads/*) from your old domain to the newdomain.

Is it possible to alias a filename on an apache webserver?

I would like to make e.g. www.address.com/u.exe equal to www.address.com/serverfile.php or pl?
Is it possible?
So if someone types www.address.com/u.exe should get servefile.php...
Thanks for showing the right direction..
This seems to work. RewriteEngine on also had to be added.
I had to change .htaccess file
RewriteEngine on
RewriteRule ^u\.exe$ serverfile.php
Yes. That's what the mod_alias Apache module does for you: http://httpd.apache.org/docs/2.2/mod/mod_alias.html#alias
Yes, it's possible with mod_rewrite like below.
RewriteRule ^/u.exe$ /serverfile.php [L]
Or below if you want to display serverfile.php (via a redirect).
RewriteRule ^/u.exe$ /serverfile.php [RL]