mod_rewrite not working to change URL - php

I want to change dynamic URL's to URL's more acceptable by search engines.
For example change this :
http://myurl.com.au/page.php?id=100&name=myname
to
http://myurl.com.au/100/myname.php
or .html at the end it does not matter.
I am using Apache 2.2. I am not using .htaccess rather I put my code in /etc/httpd/conf/vhosts/myfile
but it does not work, the URL does not change at all.
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)\.php$ /page.php?id=$1&name=$2 [L]
What am I doing wrong?

Either your have the wrong description in your question or your rule is backwards. Maybe this could work:
Options Indexes Includes +FollowSymLinks
RewriteEngine On
RewriteCond %{QUERY_STRING} id=(.*)&name=(.*)$
RewriteRule ^/page\.php /%1/%2.php [L]

After doing some further testing it turns out that I do have the right code. I just don't have my head screwed on and was not thinking. Expecting that the mod_rewrite would magically change the URL to symbolic links when it is actually doing the reverse of that. It is all working for me now.

Related

Mod Rewrite in PHP

This is my Blog page's url
www.mysite.com/blog/?id=1
www.mysite.com/blog/?id=2
www.mysite.com/blog/?id=3
But I want to look my url look like
www.mysite.com/blog/1
www.mysite.com/blog/2
www.mysite.com/blog/3
I used the following Rewriting in the .htaccess.
But I am getting Error 404 from my System.(Though the subdirectory /blog is there)
What is the mistake done by me and How can i fix this ?
RewriteEngine On
RewriteRule ^([a-zA-Z0-9_-]+)$ blog/?id=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ blog/?id=$1
Firstly check mod_rewrite is enabled on apache:
Then add the following to your .htaccess file
RewriteEngine On
RewriteRule ^/?blog/([a-zA-Z0-9_-]+)/$ /blog?id=$1 [L]
Also check AllowOverride All is given in the directory for this module in apache.
Following link can be useful:
AllowOverride for .htaccess on local machine giving 403 Forbidden
I spent almost two days last year chasing this one!
Many Linux distroes (at least Debian-based) prevent you from using a .htaccess file to override the Apache configuration files by stating AllowOverride None in the default config file.
You can change the line to AllowOverride All by editing /etc/apache2/sites-available/000-default
Hope this helps.
This is called Pretty URL's or SEO Friendly URLs, this can be achieved by several ways.
One way is doing everything yourself and modifying the .htaccess to look something like this:
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./thread.php?id=$1
RewriteRule ^search/(.*)$ ./search.php?query=$1
Note that this requires your webserver to be Apache.
Other ways (which actually work somewhat the same) is by using PHP Frameworks which often have Routing with Pretty URL's implemented so you don't have to reinvent the wheel.
This is Laravel's approach of creating routes and pretty url's:
Route::get('/blog/{id}', function() {
// Do something
});
Take a look at Laravel: http://www.laravel.com. I really love this framework and its features.
Ruby on Rails has this implemented by default as well. But I assume you are not using it since you are programming PHP.
For a good tutorial explaining how to create Pretty URL's within PHP yourself take a look at this tutorial from TutsPlus: http://code.tutsplus.com/tutorials/using-htaccess-files-for-pretty-urls--net-6049
Hope I helped you! Good luck.
I am not good in english, but here i can try to answer.
look like your blog is on a sub dir.
so,
RewriteRule ^blog/([^/]*)$ /blog/?id=$1 [L]
and try to add
RewriteBase /
if you still have problem,
also..
there are many generator for htaccess
for example
http://www.generateit.net/mod-rewrite/index.php

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.

Understanding .htaccess

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

URL Rewriting - PHP + Apache

I wanna turn PHP dynamic URLs into static URLs.
For example, I want URLs like
http://www.example.com/book.php?title=twilight to become http://www.example.com/book/twilight
and http://www.example.com/writer.php?name=meyers to become http://www.example.com/writer/meyers
When it's done, will my form validation on the site change?
My URL rewriting needs might not be much too complicated.
I'm developing it locally using XAMPP, Apache and MySql. Later I'll put it online.
How do I do that?
Is this kind of URL rewriting technique the most adviced for SEO?
You would use mod_rewrite for that. If you do a bit of searching on here for that, you'll likely find lots of other questions about how to create mod_rewrite rules similar to what you want to do.
Yep. U need to use mod_rewrite. Also do a search for .ht_access files. You can put your rewrite directives in .ht_access files and drop them in to whatever directory on your server where you want them to take effect.
For the type of rewrite you want this rule generator should be of use to you:
http://www.generateit.net/mod-rewrite/
And yes the URL you're trying to achieve is seo friendly.
You can use a .htaccess file (or better apache.conf) to forward all requests to index.php with mod_rewrite:
Options +FollowSymLinks
IndexIgnore */*
<ifmodule mod_rewrite.c>
RewriteEngine on
# if a directory or a file exists, use it directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# otherwise forward it to index.php
RewriteRule . index.php
</ifmodule>
There you can use $_SERVER['REQUEST_URI'] to interpret the request.

Little mod_rewrite problem

I have a classifieds website. Each classified is linked like this originally:
mydomain.com/ad.php?ad_id=Bmw_M3_M_tech_113620829
What RewriteRule should I use to make this link look like:
mydomain.com/Bmw_M3_M_tech_113620829
Also, what do I need to add to my .htaccess file?
This is what I have so far:
Options +FollowSymLinks
Options +Indexes
RewriteEngine On
RewriteBase /
And I have just enabled mod_rewrite which was disabled at first on my Ubuntu server by using:
a2enmod rewrite
Anything else I need to know or do?
Thanks
It looks like you need something like this:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/ad\.php
RewriteRule ^(.*)$ ad.php?ad_id=$1 [L]
This should rewrite a request to mydomain.com/Bmw_M3 into mydomain.com/ad.php?ad_id=Bmw_M3.
The RewriteCond excludes direct requests to ad.php from being rewritten. The RewriteRule would simply substitutes anything after mydomain.com/ in place of the $1. The [L] (last) flag stops the rewriting process so that it won't apply any more rewrites for a request that is rewritten by this rule.
You need to add the rule itself. Requires regex =)
RewriteRule ([a-zA-Z0-9_]+) ad.php?ad_id=$1
Of course, this regex should be fine tuned based on the ad ID you're passing on - by telling what characters can be in that ad ID and similar. For example, if all ad IDs are ending with and underscore and 9 digits, something like this:
RewriteRule ([a-zA-Z0-9_]_[0-9]{9}) ad.php?ad_id=$1
Oh, also, after enabling mod_rewrite restart apache. Make sure that AllowOverride is not set to None, cause in that case, apache will ignore .htaccess files in directories.

Categories