rid of id number in link address using .htaccess - apache

I have this link that works and leads to the correct article:
Article
however I do not want the link to display the id number I want the link to look like this:
Article
This is what I have in my .htaccess file, any help would be appreciated, thanks:
Options -MultiViews
ReWriteEngine On
RewriteBase /
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]
Is it poosible to remove the 21 id from the link but still use the id to get the variables from database. thanks.

Could you please try following, written based on samples. Please make sure you clear your browser cache after placing these urls in your .htaccess file. Also make sure this new rule is above your already existing news.php rule(shown in your samples).
RewriteEngine ON
RewriteRule ^(news)/(Hello-World)/?$ $1/21/$2 [NC,L]
##Placing OP's rule to serve urls starting with news with news.php in backend.
RewriteRule ^news/(\d+)/([\w-]+)/?$ news.php?id=$1&linkAddress=$2 [NC,L,NE]

Related

How to set the right conditions in htaccess file?

I've tried for ages to fix this problem that I have.
On my website I have rewriten some links with the htaccess file. The htaccess file currently looks like this:
RewriteEngine on
RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?sideID=$1
RewriteRule ^([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)/([a-zA-Z0-9_!-]+)$ index.php?sideID=$1&id=$2&title=$3
RewriteCond %{HTTP_HOST} ^www\.mypage\.no$
RewriteRule ^/?$ "http\:\/\/mypage\.no\/" [R=301,L]
The first rule rewrites from:
http://mypage.com/index.php?sideID=home
To this:
http://mypage.com/home
The first rule rewrites from:
http://mypage.com/index.php?sideID=b&id=12&title=a-great-blog-post
To this:
http://mypage.com/b/12/a-great-blog-post
Soo they do what I want too when it comes to rewriting I guess. BUT, the problem is that if im now navigated to any blog post on the site and then tryies to navigate back to whatever other link lets say http://mypage.com/home it will add http://mypage.com/b/12/home instead. That can of course be fixed easily by just using absolute URL's in the navigation. But when I try to make a XML Sitemap, the bot will index every page for each blog ID like so: http://mypage.com/b/12/home, http://mypage.com/b/13/home, http://mypage.com/b/14/home and so on for each blog ID and pagename.
Is there a way to make sure the RewriteRule's apply seperatly or set a condition for them or something like that?? I am a bit noob in this area.
I beg you, please help me! :)

What's the code for htaccess in my case?

The previous less described question has removed.
Specified question:
• The question have described in the statement. Please read at-least once..
• My goal is to have friendly url. So I do some actions..
it simply works with: ^thread/[(0-9)+]/(*?) $thread.php?id=$1
but not working with this: ^thread/[(0-9)+]/page/[(0-9)+]/(*?) $thread.php?id=$1&page=$2
i discovered changing 'thread' (for 2) into another word like..
^anotherWord/[(0-9)+]/page/[(0-9)+]/(*?)
$thread.php?id=$1&page=2
(above) is working fine..
but many of you are doing such..
I mean, you have
blog/id/1/blog-title.html
blog/id/1/page/2/blog-title.html
blog/edit/id/1
all for your blog.php
blog.php?id=1
blog.php?id=1&page=2
blog.php?id=1&edit
Here, id, page and blog-title can have any value. Actually id and page is a $_GET var. with this id i can pull out the blog title from db.
So what to do to get working friendly url? Please reply me..
Add this to your .htaccess in your web root / directory
RewriteEngine on
RewriteBase /
RewriteRule ^forum/(\d+)/response/[^/]*$ post.php?id=$1 [NC,QSA,L]
RewriteRule ^(thread|forum)/(\d+)(?:/page/(\d+))?/[^/]*$ $1.php?id=$2&page=$3 [NC,QSA,L]

mod_rewrite, only access one part of URL for blog entry

In other sites I've worked on, I have been able to easily generate search engine friendly URLS using the following in .htaccess:
<files entry>
ForceType application/x-httpd-php5
</files>
I'm working on a site hosted by a company that runs PHP as fast CGI and this does not work. I am trying to achieve the following URL - http://somewhere.com/blog/entry/12/this-is-the-title
I am just looking for the id (12 in the example) and do not necessarily need the title (my logic behind this being that the title might be changed by the client, links might be broken). I tried the following mod_rewrite but it does not work if I add the title:
RewriteEngine on
RewriteRule ^entry/([^/\.]+)/?$ index.php?id=$1 [L]
I've never worked with mod_rewrite before and a lot of the documentation I've come across is about achieving far more complex results. Any help would be appreciated
If the .htaccess file is in fact placed as /blog/.htaccess, you will need a RewriteBase /blog/ line.
Just looking for numbers will also help limit the returned id.
RewriteRule ^blog/entry/([01-9]+)/?$ index.php?id=$1 [L]
This also drops the '/?$' part of the regex. The '$' at the end anchors the match to the end of the string - you just need the numbers, and can ignore anything that is not a digit.

Rewrite To WordPress Page

I have an existing page of /programs/kids.php that I want to load a category page from WP. I want the .htaccess file in /programs to handle this rewriting for me.
Something along the lines of:
RewriteEngine on
ReWrite kids2.php http://www.mysite.com/blog/cat/kids/
Any help would be awesome.
If your sure mod_rewrite is enabled by apache, the it is simple as
RewriteEngine on
RewriteRule kids.php http://www.mysite.com/blog/cat/kids/
or
RewriteEngine on
RewriteRule ^programs/kids.php$ http://www.mysite.com/blog/cat/kids/
depending on location and strictness (^hhh$ matches the whole string)
The directive is named RewriteRule and not just Rewrite. So try this:
RewriteRule ^programs/kids2\.php$ /blog/cat/kids/
But if you want have requests to /blog/cat/kids/ rewritten internally to /programs/kids2.php (the exact opposite of what you’ve mentioned), try this rule:
RewriteRule ^blog/cat/kids/$ programs/kids.php [L]
You could also do an include for that kids.php in the content of an existing WP page, but you'd need the ExecPHP plugin installed.
Then kids.php would display as styled content inside a WordPress page. Which is sometimes desirable, from a site cohesiveness standpoint.
Something like this:
<?php require_once(ABSPATH. '/programs/kids.php');?>
Not exactly what you asked for, but maybe an alternative approach.

mod_rewrite to alias one file suffix type to another

I hope I can explain this clearly enough, but if not let me know and I'll try to clarify.
I'm currently developing a site using ColdFusion and have a mod_rewrite rule in place to make it look like the site is using PHP. Any requests for index.php get processed by index.cfm (the rule maps *.php to *.cfm).
This works great - so far, so good. The problem is that I want to return a 404 status code if index.cfm (or any ColdFusion page) is requested directly.
If I try to block access to *.cfm files using mod_rewrite it also returns a 404 for requests to *.php.
I figure I might have to change my Apache config rather than use .htaccess
You can use the S flag to skip the 404 rule, like this:
RewriteEngine on
# Do not separate these two rules so long as the first has S=1
RewriteRule (.*)\.php$ $1.cfm [S=1]
RewriteRule \.cfm$ - [R=404]
If you are also using the Alias option then you should also add the PT flag. See the mod_rewrite documentation for details.
Post the rules you already have as a starting point so people don't have to recreate it to help you.
I would suggest testing [L] on the rule that maps .php to .cfm files as the first thing to try.
You have to use two distinct groups of rewrite rules, one for .php, the other for .chm and make them mutually exclusives with RewriteCond %{REQUEST_FILENAME}. And make use of the flag [L] as suggested by jj33.
You can keep your rules in .htaccess.