Proper way to define url in htacces - php

I have the following htaccess file:
Rewriterule ^view_profile-(.*)$ view.php?user=$1
When I do something like "View profile it works great (URL looks like: http://home.com/view_profile-John). However, if I try this:
Rewriterule ^view/profile-(.*)$ view.php?id=$1 it says that page cannot be found.
Does anyone know why is not working?

You probably need to turn off Multiviews here. Because the beginning of the request looks like /view/... and there's a file /view.php, mod_negotiation will automatically assume you mean the php file and route it there before mod_rewrite even gets a chance to do anything. Try adding:
Options -Multiviews

Related

Error 404 when trying to create a pretty url using .htaccess in php

I want to create a pretty rule in php using .htaccess.
something like localhost/search/name instead of using the normal PHP way like search?q=name. But am stuck in implementing it because all my page are now returning error 404 and I don't know why.
Here is my .htaccess file:
RewriteEngine on
RewriteRule ^localhost/?$ search.php?name=$1 [QSA]`
Here is my search.php file:
<?php
echo $_GET['name'];
?>
So when I visit something like localhost/search/myname it returns error 404
What am I doing wrong?
You're going to want to first also include the /search/ as part of the rewrite rule and then i would also suggest adding in the 'L' flag after QSA so the rule is the last rule processed when called.
You then want to capture the name from the url and pass this through as a get param to the view.
I've also ommitted localhost from the URL as you don't need to target this.
try this:
RewriteRule ^search/(.*)$ search.php?name=$1 [QSA,L]

Simple userprofile-URL without rewriting addressrule

I'm working on a website with a lot of profiles. What I want is that you can simply navigate to one of those profiles with a URL like www.mywebsite.com/userX (just like twitter with twitter.com/userX).
I already know how this is possible by using the .htaccess and rewriterule, but the problem is it changes the entirely URL to something like www.mywebsite.com/?username=userX. It must just load a specific PHP-file without rewriting the URL in the addressbar.
Who knows how I can perform this?
(p.s. sorry for my average English)
See the fine article:
http://www.phpriot.com/articles/search-engine-urls
It explains that yeah, you want a mod_rewrite (placed in an .htaccess file) rule that looks something like this:
RewriteEngine on
RewriteRule ^/news/([0-9]+)\.html /news.php?news_id=$1
And this maps requests from
/news.php?news_id=63
to
/news/63.html
Another possibility is doing it with forcetype, which forces anything down a particular path to use php to eval the content. So, in your .htaccess file, put the following:
<Files news>
ForceType application/x-httpd-php
</Files>
And then the index.php can take action based on the $_SERVER['PATH_INFO'] variable:
<?php
echo $_SERVER['PATH_INFO'];
// outputs '/63.html'
?>

htaccess redirect files with no file extension and pass parameters to a file

http://abc.com/test/batch/profile/anyname
In the above url anyname varies depending upon the selected link.
I have to redirect the above url to friend.php and I should pass anyname as parameter to that file
How can I do this?
Put this in your .htaccess:
Options +FollowSymlinks
RewriteEngine On
RewriteRule ^profile/([a-z0-9]+)$ friend.php?username=$1 [NC,L]
This allows that people access the following url on your site:
http://www.yoursite.com/profile/abc
http://www.yoursite.com/profile/abc123
http://www.yoursite.com/profile/123
However, any other characters than alphanumerics will not qualify for the rewrite above.
Edit:
If you actually will be keeping the http://www.yoursite.com/test/batch/-url, the .htaccess would have to be adjusted accordingly. I just assumed that you'd be using the root of http://www.yoursite.com/.
You need to do something like this:
RewriteRule ^/test/batch/profile/([^/?]*)$ friend.php?param=$1

How to rewrite url using mod_rewrite

My urls currently look like this:
http://domain.com/news/articles.php?id=22&category=investments&title=securing-your-future-making-the-right-investment
How can I use mod_rewrite to make the url look more like this:
http://domain.com/news/articles/investments/securing-your-future-making-the-right-investment
EDIT: need to include the id variable too
#enable mod rewrite
RewriteEngine On
RewriteRule ^/news/articles.php?id=([0-9]+)&category=([a-zA-Z]+)&title=([a-zA-Z]+)$ /news/articles/$2/$1_$3
the ID must exist in the URL so the it looks like:
http://domain.com/news/articles/investments/{ID}_securing-your-future-making-the-right-investment
Good luck.
Add something like this to your .htaccess file:
^/news/articles/([0-9]+)/(.*)$securing-your-future-making-the-right-investment$
/news/articles.php?id=$1&category=$3s&title=$2 [L]
Don't know about the $3, but category doesn't seem to be present in the url you listed so I guess it isnt needed.
This should make it work ;)

how can I rewrite URL to fancy url?

when my user logs in, I want the get variables that were sent rewrote onto the URL like so:
http://mysite.com/mygetvar1/mygetvar_value1/mygetvar2/mygetvar_value2/
or
mysite.com/mygetvar1=mygetvar_value1/mygetvar2=mygetvar_value2/
How can I do this?
Please help! Thanks!
Codeigniter can offer you like that. Many other PHP frameworks offer that as well.
Try this.
RewriteRule /([^/]*)/([^/]*)/([^/]*)/([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
RewriteRule /([^/]*)=([^/]*)/([^/]*)=([^/]*)/ /login.php?$1=$2&$3=$4 [R=301]
First you need the mod_rewrite module enable.
After, put this in your config file :
RewriteEngine on
RewriteRule ^ads/(rims|tires|combo)/([0-9]+)/(.+).html /ad.php?noAds=$2 [L,QSA]
This is a example.
Your url will look like : http://www.yourwebsite.com/ads/rims/331/title.html
but you will call the url : http//www.yourwebsite.com/ad.php?noAds=331
For your regex , you should use a site like http://www.rubular.com
You can use the .htaccess file or put in directly in the httpd.conf file

Categories