I am trying to change my url type with .htaccess but I have a few problems. I tried a few online tools but they even do not work for me. So here what I am trying to do;
I have pages like http://mydomain.com/profile.php?u=newuser and I want to make as this: http://mydomain.com/newuser but so far I could not achieve it.
Here also what I have tried;
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) profile.php?u=$1
After making changes in .htaccess, do I also have to make any changes in my php files? Also, when try to open http://mydomain.com/newuser I noticed that some of my images on the page are disappearing, what would be the reason for that? Thank you so much guys!
You've gotten quite far, but now you're rewriting every possible url in your domain to profile.php, including stuff like /images/logo.jpg for example.
The question is, what do you want to do with this? An easy way to go about is changing it to this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) profile.php?u=$1
The added 'RewriteCond' causes the rewriteengine to only rewrite urls that don't exist on the server, so your images will show up fine.
Personally, I think it might be better to add a /profile/ prefix to all your profile urls:
Options +FollowSymLinks
RewriteEngine on
RewriteRule profile/(.*) profile.php?u=$1
This will allow you to add new rewrite rules in the future, if you need them; it will also not give you issues if one of your users decides to go for a username called 'profile.php' or anything else that clashes with the existing urls on the server.
This is because you're sending every request to profile.php (.*). This is going to affect all the requests for images, resources, etc.
Add this line above your rule to exclude "real" resources:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Related
I'll be signing businesses up to advertise on my website, and I want them to have a direct URL for their customers to go to.
Like, instead of www.website.com/page.php?id=324234234,
I want to have www.website.com/businessname
Is there a simple way to do this? I've searched and seen a whole bunch of different things people are trying to do but I haven't seen anything that's the same as what I want to do.
I'm using a VPS, and I want to make sure that I don't open up permissions so that anyone can get in there and mess things up.
Also, these users will not be signing themselves up. I will be doing that.
The simplest way to get my end result is what I'm looking for. Thanks!
Basic URL rewriting could work.
Add to your .htaccess file
RewriteEngine on
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
Then use PHP to rewrite the businessname to the ID of the company / find the data.
Of course .htaccess rewrite rules is a complete science if you need more complex rewriting...
Re-iterating what jtheman said with a little more explanation:
Create a file named .htaccess with the contents:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
You need, of course, the ability to have directory level .htaccess enabled - you're using a VPS so you should be able to do this if it is not already enabled.
So let me explain what each line will do.
RewriteEngine on
Turns on the ability to URL re-write
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Tells Apache not to re-direct files that exist in the directory already
RewriteRule ^(.*)$ page.php?businessname=$1 [L]
This is where the magic happens.
^(.*)$ this part is like a regular expression match. It will tell Apache to collect any URLs that have any characters within them and redirect them to page.php?businessname=(.*)
So, if you post:
www.website.com/stackover
It will really be sending: www.website.com/page.php?businessname=stackover
Then you can just use $_GET[businessname] to dynamically update the page.
Hope this helps!
I have a site which I have converted to use cms made simple. It works perfectly and I have the friendly urls working fine too. My issue is with a little script I wrote myself and how best to integrate it. The script is a gallery script, it reads a directory and outputs a formatted gallery in html. I was planning on making it a user defined tag in cms made simple but I hit a small snag.
The gallery script needs to be able to read in two values from the url groupId and showpage.
If I am using freindly urls then the cms and use the tag I hit a snag as the cms tries to find an actual page at "www.mysite.com/gallery/mygroup/2" and then throws a 404.
basically I need
http://www.mysite.com/gallery/photogroup/2
rewritten to
http://www.mysite.com/gallery.php?groupId=photogroup&showpage=2
UPDATE
Follwoing Yuri's advice I added his rule to the htaccess. But I have hit another snag.
So for instance if we go to
http://www.mysite.com/gallery/photogroup/2
then Yuri's rule should take effect. But that path is also a correct physical directory on my site coincidentally. Is there a way to have the rewrite rule take effect instead of bringing me to a white screen browsing the files in the directory or to the forbidden screen if I have indexes turned off which I do.
Below is my htaccess
php_flag magic_quotes_gpc Off
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
So, did you try to write in .htaccess something like this?
RewriteEngine On
RewriteCond ${REQUEST_FILENAME} !-f
RewriteCond ${REQUEST_FILENAME} !-d
RewriteRule ^gallery/(\w+)/(\d+)$ gallery.php?groupId=$1&showpage=$2 [QSA,L]
sounds like a "module" to me. Maybe this Make your module use clean URLs
I'm lost here. I'm using this script to give users the opportunity to enter their username lijke this:domain/username
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/?$ userpage.php?user=$1 [NC,L]
</IfModule>
This works fine. However, every user has pages I must link to: Video, Music, Images etc...
So I need something like:
domain/username/video
In php code it must be something like:
user.php?user=test&page=video
And one other question: What is the preferable way to link in this situation?
userpage.php?user=test&page=video
or
/test/video
And finally: Is it possible to deny the possibility to enter the url:
domain/userpage.php?user=test&page=video? Instead just always show: domain/test/video
Thanks in advance
I'm not 100% sure what you're asking? Do you need to change the rewrite rule to match the URL site.com/moonwalker/videos? You could try this:
RewriteRule ^([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Update
Just a quick note on the domain/member/videos URL structure. That could end up causing you problems in the future. For instance what if you decide to have a single page that shows all member videos? You'd probably want to URL to look something like site.com/members/videos. That's a problem, because the rewrite rule will also match that, but "members" isn't a member username.
I would probably structure my member page URLs like site.com/user/moonwalker/videos so it doesn't clash with future rewrite rules. You would change the above rewrite rule to this:
RewriteRule ^user/([^/]+)/(images|videos|music)/?$ userpage.php?user=$1&page=$2 [NC,L]
Then later on you can add a rewrite rule like:
RewriteRule ^members/(images|videos|music)/?$ allusers.php?page=$1 [NC,L]
To show all member videos.
Yes, it is possible by looking at the request line:
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /userpage\.php[?\ ]
RewriteRule ^userpage\.php$ - [F]
This is necessary as the URL path could already be rewritten by another rule and thus using just RewriteRule would match those already rewritten requests too.
I want to my adress look like this: www.example.com/112112/example
Where 112112 is a data, that I want to work with in a php script. How to do this? For some reasons, I dont want to the adress look like www.example.com?id=112112
You can use url rewrite to achieve the binding from www.example.com/112112/example to www.example.com?id=112112
For Apache webserver, you will find here a guide: http://httpd.apache.org/docs/2.0/misc/rewriteguide.html
This is about mod_rewrite and .htaccess-files, add a .htaccess-file to your webserver root and add this piece of code:
RewriteEngine On
RewriteRule ^(.*)/ index.php?id=$1
The easiest way to do this is if you're on a system using the Apache web server, along with mod_rewrite. To get the desired effect, you'd put a .htaccess in your directory with something like this:
RewriteEngine on
# Dont rewrite files or directories which actually exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for example.com/100/example to example.com/page.php?id=100
RewriteRule ^([^/]+) page.php?id=$1
You can have lots of rules to cover different possibilities.
I'm writing a website that allows people to asses a web page's readability (Flesch-Kincaid Reading Ease, thank kind of thing).
Ideally I'd like the user to be able merely to preceed the target URL with mine (like many mirror sites do), and hey presto they can see the results.
I'm guessing it's got to be done with mod_rewrite, but I'm not sure how to write it, especially given that URLs may contain so much potential junk.
How would I say:
if request is mysite.com/anything-at-all ).
redirect to mysite.com/?site=anything-at-all
Except in cases where the request is for:
just for mysite.com/
The request is for mysite.com/ajaxresponse.php?target=something
Where the request is for about.php or loading.gif
Sadly everything that I have tried so far ends up in an redirect loop...
Many thanks,
Jack
Edit your .htaccess to have:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(.+)$ ?site=$1 [NC]
The + in the regex will take care of the index page being left as is. Edit otherwise as you deem necessary (make it a 302 permanent redirect, etc...)
For excluding the specific pages you should add a line:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(ajax\.php|whatever\.gif) - [L]
RewriteRule ^(.+)$ ?site=$1 [NC]
Try this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{QUERY_STRING} !(^|&)site=[^&]
RewriteRule .+ /?site=$0 [QSA]
The first condition is to exclude requests to existing files and the second is to avoid a redirect is there already is a site URL argument.