.htaccess simple rewrite rule not working in subdirectory - php

I'm working on a subdirectory, and want to rewrite ugly file-urls into prettier URLs.
Like:
domain.com/myfolder/myreport.php --> domain.com/myfolder/MyReport/
Here's what I have:
RewriteEngine On
RewriteRule ^test/?$ index.php [NC,L]
Taken from here
However, it's not working. I've tested every combination of regex I could imagine, nothing worked.
Are there any other potential problems? Is it possible that all rules are ignored/overwritten by a rule in the root directory or something?
The .htaccess IS being used, if I add "test", I get a 500 Error.

Can you just try the following,
RewriteEngine on
RewriteBase /myfolder/
RewriteRule ^(.*)$ /myfolder/index.php?/$1 [L]

Related

Rewrite .htaccess resulting in 404

I'm trying to make my url look good but for some reasons it is not working what could be the problem here
RewriteEngine on
RewriteRule ^view/([^/]*)/([^/]*)$ /view?wall=$1&page=$2 [L]
Above rewrite should resolve like this http://website.com/view/apartment-wallpapers/1247
Alternatively
Preferably I want to make my url to look like this but it's also resulting in
http://lifistudy.com/asus-red-903.html
.htaccess rewrite rule
RewriteEngine on
RewriteRule ^([^-]*)-([^-]*)\.html$ /view?wall=$1&page=$2 [L]
Note: using openlitespeed and all .htaccess rules are working
The issue is in your rewrite rule. You're using ([^/]*) which is incorrect syntax use (.*) and your rewrite rule will work perfectly.

url rewriting with .htaccess

I'm trying to rewrite the url on a site I have on localhost (port 8000), I have already set this up for certain other directorys but I can't manage to do it for this one. I compared it to several other working .htaccess files but there wasn't any difference. (The module is activated.) The site is in a subdirectory called HTF and the file single.php works just fine. The htaccess file is placed in the same directory.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
RewriteRule videos/([0-9]+)/$ /HTF/single.php?id=$1 [L]
I would like to get an url like localhost:8000/HTF/video/1232434 for exemple.
Any help appreciated, tested several types of htacces files, here a working exemple for a other subdirectory called sorted:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteRule ^image-post/([0-9]+)/(.+)\.php$ /sorted/image-post.php? id=$1&title=$2 [L]
RewriteRule ^pages/([0-9]+)/(.+)\.php$ /sorted/page.php?page_number=$1&cat=$2 [L]
Your rule will match videos, not video as you were wanting. Also it will only match with a trailing slash.
This will work
RewriteEngine on
RewriteRule ^video/([0-9]+)/?$ single.php?id=$1
localhost:8000/HTF/video/1232434
will not match
videos/([0-9]+)/$
because of the trailing slash /. Try /? to make the slash optional or leave away. And videos.
^/HTF/video/([0-9]+)/?$ /HTF/single.php?id=$1 [L]
or
^/HTF/video/([0-9]+)$ /HTF/single.php?id=$1 [L]
This shall do the job. Sorry can not try here.

PHP clean URL with no php extension

This is my .htaccess code.
RewriteEngine On
RewriteRule ^([a-zA-Z0-9]+)/$ movie.php?name=$1
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.example\.in$
RewriteRule ^/?$ "http\:\/\/example\.in" [R=301,L]
ErrorDocument 404 /404.php
AddDefaultCharset UTF-8
Header unset ETag
FileETag None
I need a clean URL for my website.
I've consulted lots of tutorials and forums and created the above code.
But it's not working. Almost I'm fighting with code.
I don't understand the clean URL concept clearly. Are there any codings I have to write in my PHP file?
<a href='movie.php?name=titanic'> Titanic </a>
I have this link in my index.php file.
I'd like example.in/movie/titanic when I click on the link "Titanic".
Also I'd like to get the value by $_[request].
What exactly do I have to do? Please don't mark this question as duplicate, I've searched a lot and haven't got the concept clearly. Please help me out.
Thanks.
The rules in .htaccess work on url's relative to the directory your .htaccess file is in. If you have a .htaccess in your www-root, then the first argument of RewriteRule will match everything behind the domain name and before the query string (movie/titanic in http://example.com/movie/titanic?is=amovie). To fix the rule, you need to change the rule to:
RewriteRule ^movie/([a-z0-9]+)/?$ movie.php?name=$1 [L,NC]
The [L] flag stops rewriting for this round, the [NC] flag ignores case.
Besides that, you only need one RewriteEngine on, which needs to be above all rules you write. You can safely delete the second one.
You can put your .htaccess into main Directory:
RewriteEngine On
RewriteRule ^movie/([a-zA-Z0-9]+)(/)$ movie.php?name=$1 [L]
Or in movies/ directory:
RewriteEngine On /movies/
RewriteRule ^([a-zA-Z0-9]+)(/)$ ../movie.php?name=$1 [L]
While movie.php is in main Directory located.
RewriteEngine On can by starterd only once per htaccess.

RewriteRule .htaccess multiple params

I'm trying to rewrite my url, but I'm getting an error "500 Internal Server Error"
I've never done this before, and I've read some tutorials but I can't say I got any smarter from it.
The .htaccess file is in the root directory (/var/www)
Options +FollowSymLinks
RewriteEngine On
RewriteBase /sub_domain/directory_to_my_files/
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*)/$ /index.php?pos=$1:$2&text=$3
The current link goes like this:
http://sub_domain.my_domain.com/directory_to_my_files/index.php?pos=(float|integer-only):(float|integer-only)&text=(any-text)
But I'm trying to do this:
http://sub_domain.my_domain.com/directory_to_my_files/(float|integer-only):(float|integer-only)/(any-text)/
Sorry if the links is a bit hard to read.
This should be placed in /directory_to_my_files/.htaccess
RewriteEngine On
RewriteBase /directory_to_my_files/
RewriteRule ^(-?[0-9.]+)[:-]([0-9.]+)/([^/]+)/?$ index.php?pos=$1:$2&text=$3 [L,QSA,NE]
I've tested this locally:
( I did not need the rewrite base )
RewriteEngine on
RewriteRule ^([0-9.]+)-([0-9.]+)/(.*) /index.php?pos=$1:$2&text=$3 [R,L]
And it has redirected me
from
http://loc.domain/555-666/misc-text
to
http://loc.domain/index.php?pos=555:666&text=misc-text
I reckon this was what you wanted?
About the [R,L] at the end:
the R is telling apache to actually redirect, not just rewrite the url - this helps with testing.
the L is saying this is the last rule
Edit: could it be that the rewriteBase is causing you problems? Try it without and see if it works - at least you can nail where the problem is, then.

Very simple .htaccess mod_rewrite configuration gives 404

Ok, I'm starting to think that the problem is me but... ¿What's wrong here?
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It doesn't
I have a folder called "WebX" and this .htaccess inside it along with other files from the web.
mod_rewrite is working ok according to phpinfo and everything is running on localhost.
The most courious thing is that if I type localhost/WebX/testpage.html it redirects to Google
but unfortunately for me if I type localhost/WebX/mineral/1 it gives me 404. ¿What happens?
The problem you are having is caused by RewriteBase /. Your .htaccess is in a subdirectory /WebX/, but you are telling mod_rewrite to rewrite rules as if the current directory was /. It tries to load a file ver.php in your www-root, which doesn't exist. If you have verbose error messages with what file it tried to load, you'll notice it says that it tried to load /ver.php and not /WebX/ver.php as you might expect.
The rest you are doing, most notably using a relative url instead of a / prefixed absolute url, seems correct. The correct .htaccess to use would be:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /WebX/
RewriteRule testpage\.html http://www.google.com [R] #It works
RewriteRule ^mineral/([0-9]+)/?$ ver.php?id=$1 [NC,L] #It now should work

Categories