How to display post name right after domain through .htaccess - .htaccess

I have listed post form page articles which currently looks like this:
http://demo.web/articles/hello-world
and I just want to display url which will look something like this: http://demo.web/hello-world
but the problem is the page articles will fetch the data with the id passed in the URL.
And, I have also listed other article on the same page with the title other posts. I am already inside http://demo.web/articles/hello-world and if I hover over other links of the same page then it gives url which looks like this:
http://demo.web/articles/2/hello-world/articles/3/life-is-beautiful
What I mean to say is the URL is being appended in the existing URL.
At the end what I want my url to look like this: http://demo.web/hello-world or http://demo.web/life-is-beautiful
My .htaccess file have the following code:
RewriteEngine On
RewriteRule ^([a-zA-Z]+)$ $1.php
RewriteRule ^articles/([0-9]+)/([a-z-]+) articles.php?id=$1&act=$2

Its like you are rewriting as a relative path.
Try adding / in front "articles.php?id=$1&act=$2"

Related

Converting a GET parameter into a friendly URL

I have a URL: www.example.co.uk/news-and-views/article.php?Id=name-of-article
The index page of news-and-views simply spits out all news articles and a URL to view the article (which is the one above)
Basically SELECT * FROM news
Instead of news-and-views/article.php?Id=name-of-article I would like to remove the article.php?Id= so that the URL would simply be news-and-views/name-of-article
I know that in a .htaccess file you can do something like this:
RewriteRule /(.*)/$ article.php?Id=$1
But this didn't seem to work?
Am I on the right track?
Using this rule...
RewriteRule ^news-and-views/(.+) /news-and-views/article.php?Id=$1
...will masks URLs in the way you describe.
So going to http://www.example.co.uk/news-and-views/test would call http://www.example.co.uk/news-and-views/article.php?Id=test behind the scenes.

Canonical url htaccess

I am trying to achieve certain thing via htaccess file.
I would like this
http://mypage.com/e-store/561/
http://mypage.com/e-store/562/
to look like this
http://mypage.com/tv-screens
http://mypage.com/computers
I have read that it is possible via htaccess with rewrite urls. But I cant manage to wrap my head around it.
Use this is .htaccess file:
RewriteRule ^([-a-zA-Z0-9_]+)/$ ?url=$1 [L]
And add in your code output pages as:
http://mypage.com/?url=tv-screens
Where on the parameter URL the necessary page is displayed.
This link doesn't display page screen name:
http://mypage.com/e-store/561/

htaccess - get content on one url from another url

Is there the opportunity to get content on one url from another url? like this:
test.com/dir1 shows content from query test.com/getinfo/index.php?q=dir1
or
test.com/dir2 shows content from query test.com/getinfo.php?q=dir2
I need that because i must do 50 urls like test.com/dir1 ../dir50 which has the same template but with some different content based on query and one virtual url with query is better than 50 real directories.
I tried do but couldnt! Thanks in advance!
If it is all on the same host, then have a look at mod_rewrite, which can do amazing things. Just make sure you read the manual through, as there are a lot of details to take in.
RewriteEngine On
RewriteRule ^/dir1$ /getinfo/index.php?q=dir1 [L]
or more generically:
RewriteEngine On
RewriteRule ^/dir(.*)$ /getinfo/index.php?q=dir$1 [L]

.htaccess rewrite url that has already been changed

I am upgrading my site which involves new scripts and a different URL
structure. There are currently a few thousand pages so I want to
basically move them in to a subdirectory so that they are not lost.
I am not very confident with htaccess so can someone please confirm that
the first part I have done is correct:
Old URL: http://www.example.com/article/another-dir/page-group/whatever.html
RewriteRule ^article/?$ http://www.example.com/archive/ [R=301,NC,L]
To achieve this: http://www.example.com/archive/another-dir/page-group/whatever.html
Search engines will see the above as a permanent move and in the address bar
it will show the new url. Is this right?
The second part is on the new script - the url's could be improved but I am
unable to change all the script so thought about using htaccess again but am
not sure if it can be achieved like this.
At the moment the url looks like this:
url: http://www.example.com/category/4/categoryname
In the htaccess the current rewrite rule for this type of url looks like this:
RewriteRule ^category/(.*)/(.*)$ category.php?id=$1&slug=$2
Is it possible to change this so that in the url address bar I end up
with this:
http://www.example.com/categoryname
Basically I don't want to see either the number or category/ in the resulting
url as it will be easier for visitors to use - is that possible??
Thanks in advance for any help.
The second question related to passing in URI components for querystring redirect and then hiding those components in the URL I don't think would be easy, if even possible, using RewriteRules.
For the first question though. Given the sample URLs you mentioned, the RewriteRule would need to include capture and backreference if you want to preserve the full URL in the redirection. For example:
RewriteRule ^article/?(.*)$ http://www.example.com/archive/$1 [R=301,NC,L]

Rewriting different URL from one page to another using .htaccess

I need to know if there is a way to redirect long URLs to short URLs.
I have an RSS page that will list out a number of news items from my website. The URL format of each item (as per MySQL query) is something like http://example.com/news.php?id=2.
On my news.php (after being redirected to the news page upon clicking on any news title on the RSS page), there are parameters set to carry along the IDs which looks like http://example.com/news.php?news=2&view=1&topic=12.
For SEO purpose, I need to erase that dirty query string so that viewers will only see http://example.com/news.php?id=2 while on the server-side, it actually reads http://example.com/news.php?news=2&view=1&topic=12.
I had created an .htaccess file and it is placed inside the rss folder (where the rss.php file is located ) and tried several attempts but to no available.
Help me on this.
Here's what I would do:
RewriteEngine on
# id + view + topic
# http://www.domain.com/news/1/1/12 will load: /news.php?news=1&view=1&topic=12
RewriteRule ^news/([0-9]+)/([0-9]+)/([0-9]+)(/?)$ /news.php?news=$1&view=$2&topic=$3 [QSA,L,NC]
Please this .htaccess in the root folder.
Should the parameters be assigned to ($1,$2,$3...etc) instead?
RewriteRule ^news(/?)$ /news.php?news=$1&view=$2&topic=$3 [QSA,L,NC]

Resources