Why this RewriteEngine rule is not working? - .htaccess

I'm trying to understand how to do an URL rewrite using .htaccess file.
The easier-to-digest information that I've found so far is here, and I'm trying to adapt it to my own needs, but unsuccessfully so far.
I have this url:
http://www.example.com/contenido.php?id=19
and I want at the moment to transform it into:
http://www.example.com/contenido/19
I've tried an .htaccess file with:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^contenido/([0-9]+) http://www.example.com/contenido.php?id=$1 [NC]
When I try the clean url I get a "server not found" error.
Any insight will be very appreciated.
Please note that RewriteEngine is ON in my Apache server.

It is not necessary to test if the pages exist, as you rewrite all the contents of the directory.
You can use:
Options +FollowSymLinks
RewriteEngine On
# To externally redirect contenido.php?id=nnn to contenido/nnn
RewriteCond %{THE_REQUEST} \s/+contenido\.php\?id=([^\s&]+) [NC]
RewriteRule ^ /contenido/%1 [R=301,L,NE]
# To internally rewrite contenido/nnn to contenido.php?id=nnn
RewriteRule ^contenido/(\d+/?)?$ contenido.php?id=$1 [NC,L]

After a little help from this tutorial, I've found a solution.
Just in case it helps someone I'll post it here:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^contenido/(\d+)*$ ./contenido.php?id=$1

Related

.htaccess change parameters received directly after domain

I have been looking for many tutorials bu no one has really helped me, I know you guys can:
My website has the following structure:
mywebsite.domain/?pg=somepage
But I would like to leave it like this:
mywebsite.domain/somepage
All the tutorials I have found work in the next structure:
mywebsite.domain/file.php?pg=somepage
So I can't gess how a RewriteRule would be using my structure.
I have tried:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php/?pg=$1
And also:
RewriteEngine on
RewriteRule ^index/([0-9a-zA-Z]+) index.php?pg=$1
But I have had no luck :(
Note: My index file has .php extension
You could do something like this -
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
This will redirect mywebsite.domain/somepage to mywebsite.domain/index.php?pg=somepage
or full config -
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?pg=$1 [L]
</IfModule>
for more info - Apache mod_rewrite

.htaccess rewrite insert new url segment e.g. www.domain.com/wildcard == www.domain.com/new_segment/wildcard/

I would like to do this globally via htaccess rewrite so any www.domain.com/wildcard/ would == www.domain.com/new_segment/wildcard/ but I can't seem to figure this out.
So far, I have the following:
Options +FollowSymLinks -MultiViews
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain\.com\/new_segment\/$1" [R=301,L]
For some reason, on the initial page load I get www.domain.com/new_segment/new_segment/ but if I go to www.domain.com/old_segment I would expect www.domain.com/new_segment/old_segment/ but I get www.domain.com/old_segment/ instead.
Any help would be greatly appreciated.
Why would you want that? Anyways, you can do it easier by putting desired files into a directory and a redirection from a file on the /old_segment to /new_segment.
This can be done with:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/new_segment
RewriteRule ^(.*)$ /new_segment/$1 [R=301,L]
It will check to see if the new_segment isn't in the url, if it is then nothing will be redirected. If it isn't then new_segment will be added.

Url changed but page remains same

I want to change my url
http://www.abc.com/search_result.php?id=110
to
http://www.abc.com/110
Here is the code which i am using.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^\s]+) [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
But the problem is, url changed to http://www.abc.com/110 , but page remain same.
Please anybody help !
One thing more i want to ask . Suppose i want to add more parameter in original url:
Say,
http://www.abc.com/search_result.php?id=110&name=amit
then what i should do to get the result.
http://www.abc.com/i-am-amit
Thanks !
You need an internal rewrite rule also for showing actual content from search_result.php"
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+search_result\.php\?id=([^&\s]+)\s [NC]
RewriteRule ^ http://abc.com/%1? [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ search_result.php?id=$1 [NC,L,QSA]
Also for http://www.abc.com/search_result.php?id=110&name=amit how do you want pretty URL to be? Keep in mind that you will need both id & name in pretty URL such as:
http://www.abc.com/110/amit
Is that how you want?

Htaccess /imagename/delete

Okay so i'm working on creating a thing to delete images from my server. I figured out about the unlink function and have turned it into a get variable. How can I use htaccess to delete a file from my server with a url like this example.com/imagename/delete
RewriteRule ^/delete/([^/]+)$ index.php?delete=$1
I realized that this does /delete/imagename but I want the urls to be /imagename/delete. I tried the following but that didn't work.
RewriteRule ^/([^/]+)$/delete index.php?delete=$1
I know this sounds simple and easy to find on the web but I can't seem to find this!
Change your rules to
Options +FollowSymLinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteRule ^([^/]+)/delete/?$ index.php?delete=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(([^/]+/)*[^.]+)$ /$1.png [L]

mod_rewrite rule for exact expression not working

I don't work to much with mod_rewrite so I could easily be just missing something simple. I want to match and replace a specific URL. The URL I want to rewrite would look like this:
http://www.sample.com/hello to http://www.sample.com/index.php?page=hello
My htaccess currently has the following rule in place:
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
However, when I implement the rule and I test it by trying to go to http://www.sample.com/hello or http://www.sample.com/hello/ I always get my 404 error document. ugh I feel like it should be simple and I must be missing something right there. Any help would be fantastic and much appreciated!
Here is my whole file for reference:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_REFERER} !^$
RewriteCond %{HTTP_REFERER} !^http://(www\.)?sample\.com/ [NC]
RewriteCond %{REQUEST_URI} !hotlink\.(gif|png) [NC]
RewriteRule ^(.*)\.(gif|jpe?g|png)$ sample.com/hotlink.png[R,NC,L]
RewriteCond %{HTTP_HOST} ^www.sample\.com$ [NC]
RewriteRule ^(.*)$ http://sample.com/$1 [R=301,L]
RewriteRule ^(hello)/?$ index.php?page=$1 [L]
Thanks
Your code works perfeclty fine for me. Maybe you're missing
RewriteEngine On
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
or maybe mod_rewrite is not installed or loaded at all.
Look for
LoadModule rewrite_module libexec/apache2/mod_rewrite.so
in your main apache configuration file httpd.conf
This is one of those times...
I cleared the htaccess file and only used the following couple lines and tested it out:
Options +FollowSymlinks -MultiViews
RewriteEngine on
RewriteBase /
#
RewriteRule ^(hello)/?$ page.php?page=$1 [L]
I realized looking at the non custom 404 page that it referenced page.php and not index.php!
Not Found
The requested URL /page.php was not found on this server.
So turns out with all my edits I mistakenly typed page.php in the rule instead of index.php. I corrected it and the rule works. One of those times :)
Thanks all!

Resources