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
Related
I've searched everywhere trying to find a solution to something I thought was a particularly common problem, but I can't seem to find anything that works.
I'm using mod_rewrite to redirect all requests via my own CMS (index.php). Here's the htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
(The above mod_rewrite is exactly the same as the code found in a standard Wordpress installation which is why I'm perplexed as to how I cannot find a solution for the following problem)
I want to add some image processing to my video thumbnails. I have written a php script (video-image.php) that works great but I cannot seem to find a means to redirect the images urls whilst keeping the index.php redirect intact. The code I'm looking for does something like this:
RewriteRule ^images/video/(.*)/(.*).jpg /images/video/video-image.php?video_image_id=$2&video_image_width=$1 [NC]
But the above code seems to clash with the mod_rewrite that sends my requests to index.php. It seems I can either redirect the images to video-image.php or redirect my pages to index.php but there must be a way to do both?
I've always found mod_rewrite confusing so apologies if I'm not explaining myself clearly enough. All php scripts function perfectly well without the mod_rewrite so I'm certain it's not a php issue.
If anyone can shed any light on this problem or point me to an answer I'd really appreciate it!
The gist of your issue was the ordering of rewrite blocks. You usually put the more specific ones above the general rules. In your case:
# specific
RewriteRule ^images/video/(.*)/(.*).jpg /images/…
# generic
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
A better approach for Apache 2.4 and later is to use FallbackResource instead of the RewriteCond/Rule blob:
RewriteRule ^images/video/(.*)/(.*).jpg /image
FallbackResource index.php
Two more things:
<IfModule mod_rewrite.c>
</IfModule>
Is something that you should not commonly use. Conditional directives make sense in Apaches core configuration. For .htaccess RewriteRule blocks it's less advisable. It's unlikely that mod_rewrite randomly disengages at runtime. And if ever, you'd rather want HTTP 500 errors in your log instead of users seeing 404 Not Found results.
Also RewriteBase can shorten some rules, or abstract their residence. But you also shouldn't use it habitually. Instead just prefix blog/ to your match rules, and assemble all rewrites in the DOCUMENT_ROOT/.htaccess (or better yet VirtualHost section for performance).
I want to be able to open the following url
http://example.com/login/12345
and in the background, it should load:
http://example.com/index.php?endpoint=login&access_key=12345
Obviously, I have tried many htaccess generators to create an appropriate RewriteRule set.
For example the following (from: http://www.generateit.net/mod-rewrite/index.php):
RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
Even though I know for a fact that .htaccess and mod_rewrite is enabled on my server (i tried to setup a test to redirect all requests from example.com to example2.com using htaccess and mod_rewrite, and it worked), I can't get this to work.
I have now spent nearly 2 hours to find for a solution on stackoverflow and other websites, but all my attempts failed.
I hope someone can help me find out the correct way to rewrite my url in the above example.like explained above.
Thanks a lot in advance!
Try with this:
RewriteEngine On
RewriteRule ^([^\/]*)/([^\/]*)$ /index.php?endpoint=$1&access_key=$2 [L]
#-Added-these---^--------^
When tested, here is the result:
Add this to the .htaccess file in your DOCUMENT_ROOT
Assuming login is not variable
RewriteEngine On
RewriteRule ^login/(\d+)$ index.php?endpoint=login&access_key=$1 [L,NC,DPI]
** If login is variable**
This is in case you want to redirect not only for login, but also for urls such as http://example.com/housenumber/12345
RewriteEngine On
RewriteRule ^([^/]+)/(\d+)$ index.php?endpoint=$1&access_key=$2 [L,NC,DPI]
Tested on actual Apache 2.2 and 2.4 :)
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.
Ok i guess this is a bit of a general question really as opposed to a problem.
I am building a system where users can share their profile on other websites so i am wondering is it possible to shorten the actual url which would provide a link to their profile which would be something like this, www.somedomain.com/users/profile.php?user=myusername to simply cut out the users folder and the profile page and so something like this: www.somedomain.com/myusername
I have seen lots of url shortening scripts but they don't seem to do this, any suggestions or advice would be appreciated.
Thanks
What you're looking for is called URL rewriting and can be done using Apache's mod_rewrite. You would place a file called .htaccess in your root web directory and it would contain a snippet like this:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule /(.*) /users/profile.php?user=$1
this is called "url rewriting" - there are different approaches to do this, for example using apaches mod_rewrite. another way would be to manually parse $_SERVER['REQUEST_URI'] - this would make your site work even if mod_rewrite isnt enabled, but is some more work.
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.