How to have a post variable defined without '?{name}=' in the url? - php

Currently on my website, my profile urls look like this:
../user/?id=1
I would rather them look like this:
../user/1
How would I go about this? I'm running this with Apache and PHP if that is important to the question.

You can do this with .htaccess
Example:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^user/([a-zA-Z0-9_-]+)$ user.php?id=$1 [L,QSA]
</IfModule>
This htaccess rule will redirect input like www.site.com/user/123 to user.php?id=123

Related

.htaccess modrewrite not redirecting my links

I'm changing my website links to a SEO friendly urls, I did every thing in php file and changed the urls as following:
http://mywebsiet.com/news-details.php?id=2012/6/21/newstitle.html
how can I redirect to:
http://mywebsiet.com/2012/6/21/newstitle.html
I tried this Generating tool from www.generateit.net/mod-rewrite/
and created the .htaccess with the code provided:
RewriteEngine On
RewriteRule ^([^_]*)$ /news-details.php?id=$1 [L]
and even so, nothing get changed.... any ideas?
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 ^(.+?\.html)$ /news-details.php?id=$1 [L,QSA,NC]

Rewriting urls with mod_rewrite - GET variables

I found out I could make clean urls with mod_rewrite. I managed to make pages like /home.php viewable by visiting /home (without .php).
Now, I'd like to turn view_album.php?album_id=23 into album/23
This is the code I use, but sadly it's not working:
Options SymLinksIfOwnerMatch MultiViews
RewriteEngine On
RewriteBase /beta/
RewriteRule ^album/(.*)/ view_album.php?album_id=$1
Thanks in advance.
use
RewriteEngine On
RewriteBase /beta/
RewriteRule ^album/([0-9]*)$ view_album.php?album_id=$1
and make sure you only rewrite if a noting or a number follows album/, so your can access your images, which may be in a folder named album.

modrewrite in subfolder

Just wanted to know if is possible to use mod_rewrite on a single(or more ) sub folder(s).
have a service in a folder that i would like to call like a rest service syntax .
$url = 'http://mysite/subfolder/parem1/parem2/parem3/';
$_SERVER['REQUEST_URI']
$tokens = explode('/', $url);
echo $tokens[sizeof($tokens)-2];
so the question is if I could catch all the different segments in a index.php in the subfolder, by using forexample modrewrite ( by placing a .htaccess with the rules only in this folder ? ) or how is it done ?
regards
If you want to define rewriting rules for a subfolder, take a look at RewriteBase
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yoursubfolder
RewriteRule ^(.*)$ index.php?$param=$1
</IfModule>
It is possible. In your .htaccess, you could put something like this:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^([^/\.]+)/?$ index.php?p1=$1
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?p1=$1&p2=$2&p3=$3&p4=$4
</IfModule>
In your case you could just put the .htaccess file with the index.php in the folder where your service is located.
This way you can fetch (and sanitize) the $_GET array, instead of fetching the whole url.

.htaccess image redirect

I've written a PHP class to add a watermark to an image, it works great when accessed via URL directly. I would like to "redirect" every image to my URL like so:
http://www.mysite.com/img.jpg
to this
http://www.mysite.com/watermark/watermark.php?image=http://www.mysite.com/img.jpg
Basically what I want is to pass the src attribute specified in the HTML to the image variable. I'm currently struggling with my .htaccess file and I can't get it working, here it is:
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([^_].*\.(gif|jpg|png))$ /watermark/watermark.php?image=$1 [L]
Looking forward to your replies and thanks in advance!
Try this:
RewriteEngine On
RewriteBase /
RewriteRule ^(.*)\.(jpg|png|jpeg|gif)$ watermark/watermark.php?image=$1.$2 [NC,L]
Try adding -MultiViews to your options line, so it reads as:
Options +FollowSymLinks -MultiViews

Creating friendly URLs with mod_rewrite in htaccess?

I have searched this forum and have done extensive google search and have not been able to figure it out. I have many links that look like:
mysite.com/example6.php?id=play-game
mysite.com/example6.php?id=watch-tv
mysite.com/example6.php?id=go-outside
I have tried lots of different code putting this in my .htaccess file:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteRule ^([^/]*)\.html$ /example6.php?id=$1 [L]
and
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^id/(.*) example6.php?id=$1
</IfModule>
and many more attempts but nothing works to get my URLS to look like this:
mysite.com/example6/play-game
mysite.com/example6/watch-tv
mysite.com/example6/go-outside
I do have mod_rewrite enabled on my server. Does anyone have any ideas?
Try this:
RewriteRule ^example6/(.*)$ example6.php?id=$1 [L]
Your first example (RewriteRule ^([^/]*)\.html$ /example6.php?id=$1 [L]) would correctly work for urls like "/play-game.html", and your second example (RewriteRule ^id/(.*) example6.php?id=$1) would correctly work for urls like "/id/play-game".

Categories