.htaccess RewriteCond external links - apache

I've been searching, reading and spent the last 8 days trying to figure out converting dynamic links to "pretty" links.
I starting using .htaccess and rewrite rules.
I have this basic code in my .htaccess file:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^pc-download/([0-9]+)/([^/]*)\.html$ /cgi/pc_read\.pl\?show=$1 [NC]
This will internally redirect my links to the proper cgi file and returns the item.
The problem I'm having is for external links in search results. From what I've read & read & read, I should be using the RewriteCond %{THE_REQUEST} and then a RewriteRule.
Since I've changed how my links are structured, I don't know if what I'm looking to do is even possible.
In the RewriteRule for internal links, the first part is "hard coded" ie, I make pc-download part of the code. Next ([0-9]+) is the item ID which is the variable ?show=$1. Here is the tricky part, ([^/]*) is an asset name that is in the database but not in the original (old) url. In the internal links, I have it coded in the page so any links in my pages automatically get generated.
So, here is the way I would like the external link to go:
External link : www.xyz.com/cgi/script.pl?show=001
Landing page : www.xyz.com/pc-download/001/name-of-product.html
I looked at maybe using RewriteMap. I created a txt file with the " ID Name Of Product " inside but putting that in my .htaccess file kills the whole website without even having a RewriteRule active.
Am I just spinning my head for nothing or am I headed down the right path?

Wasn't able to figure out how to get the external link to go to a new page & get the new link structure/variable so I modified the .htaccess:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^pc-download/([0-9]+)/([^/]*)\.html$ /cgi/pc_read\.pl\?show=$1&mytemplate=Moved [NC]
Updated the old template page with a meta refresh:
<META http-equiv="refresh" content="0;URL=http://www.example.com/pc-download/[[gameid]]/[[assetname]].html">
This gets the 2 variables [[gameid]] & [[assetname]] from my database using the cgi script that runs my website and redirects to the proper URL structure.
Then, I created a new page that displays the old database information like before and all the URLs match up in the browser.
It isn't an ideal solution but it seems to work. I hope that the meta refresh lets the search engines know that is a permanent change/move and I'll be able to delete that step/page in a couple months.

Related

Rewriting dynamic url's using apache rewrite

I have a anchor tag in my html content like this class="list-content" href="/abcd/test.html".
and this is in a lot of places in my html for a list of some results.
I need to append all these URLs that are in "href" by appending a prefix.
For example: /abcd/test.html should be dynamically changed as newprefix/abcd/test.html
If i have another one like /xyz/some.html then this should be changed as newprefix/xyz/some.html
I have explored different solutions over the internet and I have not found something that would fit my problem.
To implement an external redirect to prepend /newprefix to these requests you could do something like the following near the top of the root .htaccess (or server config).
For example:
RewriteEngine On
RewriteRule ^[^/]+/[^./]+\.html$ /newprefix/$0 [R=302,L]
The above will redirect requests for /abcd/test.html or /xyz/some.html to /newprefix/abcd/test.html and /newprefix/xyz/some.html respectively. Anything that matches the pattern /<something>/<file>.html.
$0 is a backreference that contains the URL-path that is matched by the RewriteRule pattern.
Note that this is not "url-rewriting" since you stated in comments that you do not want to "hide" the /newprefix part of the URL from your users. An external redirect is therefore the only solution if you are intending to use Apache / mod_rewrite (as tagged).
Aside: This is not particularly good for SEO, your users or your server since the user is externally redirected everytime they click one of your links, potentially doubling the number of requests that hit your server and slowing your users.

Htaccess one and two parameters

I'm a newbie in htaccess and I'm having a problem. See my htaccess code below.
RewriteEngine On
RewriteRule ^([^/]*)/lesson/([^/]*)\.htm$ /thesis/index.php?page=$1&lesson=$2 [L]
RewriteRule ^([^/]*)\.htm$ /Thesis/index.php?page=$1 [L]
When the user click or navigate to a link which is having two parameters. The page is giving the requested page correctly. The URL looks like this http://www.example.com/home/lesson/1.1.htm
But after that, when the user click the link which is having one parameter for example is the logout script. everything goes wrong. the URL became http://www.example.com/home/lesson/logout.htm
I hope someone could help me.
Looks like you have relative URLs and because of the /'s in the URL, you're base is messed up. You should either change all your links to absolute URLs, or include the base URI in the header of your pages:
<base href="/" />
Because the URL you're accessing is: http://www.example.com/home/lesson/1.1.htm, and as far as the browser knows, the "page" is 1.1.htm and the "path" is /home/lesson/. Thus, if the browser sees a relative URL (something that doesn't start with an "http" or "/"), it needs to append a base to the beginning in order to properly resolve it. Since the browser thinks the path is /home/lesson/, it attaches that to the beginning.

apache RewriteRule from root to subdirectory missing links

i made a RewriteRule in my .htaccess to redirect the following pattern www.mysite.com/s/1 to www.mysite.com/site/core/gallery.php?s=1, the redirect works but the page loads with missing links how can i fix this
this is my rewrite rule:
RewriteEngine On
RewriteRule ^s/([A-Za-z0-9-]+)/?$ /site/core/gallery.php?s=$1 [NC,L] # Handle scene requests
Most likely what you refer to with the expression "with missing links" is that objects referred to via links inside that page are not loaded once the page itself is loaded. This can have a number of reasons, since you do not provide any details we have to guess...
Most likely the reason is that your "links" are of wrong format, absolute instead of relative. Take a look at the format of the links that are not loaded. Probably they are either full urls including protocol scheme and server name or an absolut path (starting with a /). Since you redirect the request but most likely not requests to those embedded links things don't match.
Have a try changing the links to relative format (not starting with a leading /), so that the referenced objects can be found relative to the pages position in the namespace of the server.

Multiple Domains to Display Content from Landing Pages on Another Domain

We have created a bunch of landing pages on a Joomla CMS system, such that the URL for each landing page is www.domain.com/page1.html and www.domain.com/page2.html, and so on. Of course the page1.html isn't really an HTML file it is a dynamic CMS page, just rewritten with htaccess.
The goal is to have one of our other domains, something like www.uniquedomain1.com show the content of www.domain.com/page1.html. Or, another domain like www.uniquedomain2.html show the content of www.domain.com/page2.html.
This needs to be search engine friendly so we can't use URL masking. Also we can't use HTACCESS redirects as this actually changes the URL in the browser bar. Need to keep the www.uniquedomain1.com URL in the browser bar.
Tried Apache VirtualHost options without any luck. You can park in a directory but not from a URL.
Ended up parking the domains on one folder, and then creating a PHP script to detect the domain host and then use CURL to query the correct url and deliver content. This whole thing seems ridiculously over complicated, and of course CURL isn't the best option, but it is all we could get to work.
Any thoughts on how to do this, or a better solution?
You can use HTACCESS redirect rules to do it without performing a redirect.
Change the html file names to be the domain name of the desired domain like domain.tld and do something like this in an .htaccess file
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9\.-]+\.[a-z]+) [NC]
RewriteRule ^$ /%1.html [L]
A quick test of this worked for two of my test (sub)domains test.domain.tld and test2.domain.tld. Both properly redirected to files with the names test.domain.tld.html and test2.domain.tld.html without modifying the URL.
You could also just use your PHP wrapper script to grab the content of each of the miscellaneous html files and output them.
If you renamed all of your HTML files (as in my previous suggested answer) to be domain.tld.html you could do it fairly easily. Something might look like:
<?php
require($_SERVER['SERVER_NAME'] .'.html');

Dynamic URLs + SEO

I have a website that generates dynamic urls for all content.
It uses a function to show the content on index.php page and it's called by "action".
So, if the user wants to signup, there is a signup function calling it.
If the user want's to browse downloads page, there is a downloads function calling it.
Something like this:
index.php?action=signup
index.php?action=downloads
index.php?action=news&type=type&id=id
In the last case are the news.
The news are divided by type and ID, both numerics.
Type can be: article, update, etc.
My question is: HOW and WHAT must I do in order to transform it all on friendly URLs?
Presuming you are on an apache server, you will want to use a .htaccess file, and a method of using rules to rewrite URLs, not surprisingly called RewriteRule.
There's a ton of information through google if you search for .htaccess RewriteRule, but a great starting point is http://corz.org/serv/tricks/htaccess2.php.
You will essentially end up by putting a file called .htaccess in your public web files folder, with something like this:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^(?:/(.*)){1,3} index.php?action=$1&type=$2&id=$3 [R,NC]
Note: I've not tested the above, and it will differ depending on how you want your pretty link to look.